如何在ProcessPoolExecutor中获取超时参数,以在“超时”秒后返回错误?

时间:2019-01-26 00:53:11

标签: python parallel-processing python-multiprocessing

我试图了解concurrent.futures.ProcessPoolExecutor包中的timeout参数如何工作。我面临的问题是进程运行的时间超过executor.map()中的timeout参数指定的时间。

我的代码如下:

import time
import concurrent.futures

def sleeper(duration):
    time.sleep(duration)
    print("slept for duration ", str(duration))
    return duration * -1

def main():
    times = [1, 4, 15, 3]

    with concurrent.futures.ProcessPoolExecutor(max_workers=6) as executor:
        for result in executor.map(sleeper, times, timeout=10):
            print(result)

if __name__ == "__main__":
    main()

由于超时时间是10秒,所以我本来会排除打印功能

slept for duration  1
-1
slept for duration  3
slept for duration  4
-4

紧接着是超时错误,因为在该函数完成15秒钟的睡眠之前已达到10秒钟。相反,我得到的是这样:

slept for duration  1
-1
slept for duration  3
slept for duration  4
-4
slept for duration  15
Traceback (most recent call last):
  File "debug_processpoolexecutor.py", line 17, in <module>
    main()
  File "debug_processpoolexecutor.py", line 13, in main
    for result in executor.map(sleeper, times, timeout=10):
  File "C:\Users\user\AppData\Local\Continuum\anaconda3\lib\concurrent\futures\process.py", line 476, in _chain_from_iterable_of_lists
    for element in iterable:
  File "C:\Users\user\AppData\Local\Continuum\anaconda3\lib\concurrent\futures\_base.py", line 588, in result_iterator
    yield fs.pop().result(end_time - time.monotonic())
  File "C:\Users\user\AppData\Local\Continuum\anaconda3\lib\concurrent\futures\_base.py", line 434, in result
    raise TimeoutError()
concurrent.futures._base.TimeoutError

如您所见,该函数实际上会休眠15秒钟,然后返回超时错误10秒钟。这违反了终止卡住的进程(在我的实际应用程序中)的目的。

为什么它允许该功能完成15秒钟的运行?如果异常运行时间超过10秒,而又不必等待15秒,如何获取它以返回异常?

0 个答案:

没有答案