I am learning concurrent.futures
(moving from threading
) and I fail to understand how exactly the submission and waiting work.
Consider the following code, there are two threads started, one of them supposedly immediately finishes while the other indefinitely hangs.
My expectation was that both threads would be stared via submit()
and the control immediately given back to the main program.
After that wait()
with a timeout would return a tuple of done
and not_done
threads (the not_done
one would be the one forcefully interrupted by the timeout).
import concurrent.futures
import time
def worker(wait_infinite):
if wait_infinite:
time.sleep(100)
with concurrent.futures.ThreadPoolExecutor() as executor:
inf = executor.submit(worker, True)
not_inf = executor.submit(worker, False)
res = concurrent.futures.wait([inf, not_inf], timeout=2)
print(res)
What happens is that the execution hangs on not_inf = executor.submit(worker, False)
. Why isn't control given back to the main program?