TIL - In Python concurrent.futures.as_completed Yields Tasks as Soon as They Finish
When executing multiple tasks in parallel with thread or process pools, as_completed gives you the results in the exact order they finish, not the order they were started.
from concurrent.futures import ThreadPoolExecutor, as_completed
import time
def slow_task(n):
time.sleep(n)
return n
with ThreadPoolExecutor() as executor:
# Submit tasks with different sleep times
futures = [executor.submit(slow_task, t) for t in [3, 1, 2]]
# 1 prints first, then 2, then 3
for future in as_completed(futures):
print(f"Finished sleeping for: {future.result()}s")
This prevents fast tasks from being blocked in a list comprehension waiting for an earlier, slower task.