How does concurrent.futures.as_completed work? How does concurrent.futures.as_completed work? python-3.x python-3.x

How does concurrent.futures.as_completed work?


I want to know how it works internally.

as_completed sets up a callback to fire when the future is done, doing so for all the futures it receives. (It uses an internal API equivalent to add_done_callback for this purpose.) When any of the futures completes, as_completed is notified by its callback being run. The callback runs in whatever thread it was that completed the future, so it only sets an event, which is shared by all callbacks, and which as_completed sleeps on. Once woken up by the event, as_completed immediately yields the finished future. This is how as_completed ensures that futures are yielded as they are completed, regardless of the order in which that happens. After yielding, the event is cleared and the waiting is repeated until all the futures are done.

Is it yielding completed tasks (futures) immediately?

Yes, that follows from both the documented interface and the implementation.