Why doesn't concurrent.futures.ThreadPoolExecutor().submit return immediately?

元气小坏坏 提交于 2019-12-22 19:45:12

问题


In this code:

import concurrent.futures
import time

def pafter(t):
    time.sleep(t)
    print('Hi')

with concurrent.futures.ThreadPoolExecutor(5) as e:
    e.submit(pafter, 2)

print('With returned')

I expect to see:

With returned
Hi

but I see:

Hi
With returned

Why doesn't submit return immediately? What do I change to make it do so?


回答1:


Using the with statement is equivalent to calling executor.shutdown(), which is documented like this:

shutdown(wait=True)

Signal the executor that it should free any resources that it is using when the currently pending futures are done executing. Calls to Executor.submit() and Executor.map() made after shutdown will raise RuntimeError.

If wait is True then this method will not return until all the pending futures are done executing and the resources associated with the executor have been freed. If wait is False then this method will return immediately and the resources associated with the executor will be freed when all pending futures are done executing. Regardless of the value of wait, the entire Python program will not exit until all pending futures are done executing.

You can avoid having to call this method explicitly if you use the with statement, which will shutdown the Executor (waiting as if Executor.shutdown() were called with wait set to True)

The bold sections explain the behavior you're seeing; the submit() call does return immediately, but the with statement will block until all submitted work is done. To change it, you need to not use the with statement, and instead explicitly call shutdown(wait=False).



来源:https://stackoverflow.com/questions/51502812/why-doesnt-concurrent-futures-threadpoolexecutor-submit-return-immediately

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!