期货行为不一致。ProcessPoolExecutor

时间:2018-10-02 23:00:42

标签: python python-3.x concurrency future

正在考虑以下代码:

from concurrent import futures
import os
import signal


with futures.ProcessPoolExecutor(max_workers=2) as ex:
    print('getting the pid for one worker')
    f1 = ex.submit(os.getpid)
    pid1 = f1.result()

    print('killing process {}'.format(pid1))
    os.kill(pid1, signal.SIGHUP)

    print('submitting another task')
    f2 = ex.submit(os.getpid)
    try:
        pid2 = f2.result()
    except futures.process.BrokenProcessPool as e:
        print('could not start new tasks: {}'.format(e))
    except Exception as e:
        print(str(e))

输出不一致。从我阅读的文档来看,它的行为应该是:

“如果工作进程之一发生某种故障导致其意外退出,则ProcessPoolExecutor被视为“中断”,将不再计划任务。”

应该以该脚本为例。相反,如果我多次运行代码,则会得到以下输出:

(bot-LP2ewIkY) ⋊> ~/w/p/b/bot on master ⨯ python brokenPool.py                                                                                                                                                                   18:54:55
getting the pid for one worker
killing process 4373
submitting another task
could not start new tasks: A process in the process pool was terminated abruptly while the future was running or pending.
(bot-LP2ewIkY) ⋊> ~/w/p/b/bot on master ⨯ python brokenPool.py                                                                                                                                                                   18:54:56
getting the pid for one worker
killing process 4443
submitting another task
could not start new tasks: A process in the process pool was terminated abruptly while the future was running or pending.
(bot-LP2ewIkY) ⋊> ~/w/p/b/bot on master ⨯ python brokenPool.py                                                                                                                                                                   18:54:57
getting the pid for one worker
killing process 4514
submitting another task  <----- (No exception thrown after this)

如您所见(箭头指向不应发生的情况),它仅在一部分时间内抛出异常。这是错误吗?

注意:这是我试图得到回答的相关问题,导致我发现了这个问题:ProcessPoolExecutor, BrokenProcessPool handling

更新:已报告给Python:https://bugs.python.org/issue34877

0 个答案:

没有答案