Reputation: 3977
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?
Upvotes: 6
Views: 3758
Reputation: 94871
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)
.
Upvotes: 11