as_completed: Don’t Wait for Everyone to Finish
While gather()
makes you wait for all tasks to finish before getting any results, as_completed()
gives you results as soon as they’re ready. This is perfect when you want to start processing results immediately.
How as_completed Works
The as_completed()
function takes an iterable of coroutines or tasks and returns an iterator that yields futures as they complete.
|
|
Running this code, you’ll see that results are processed in the order they complete, not in the order the tasks were created.
The Restaurant Analogy
Think of as_completed()
like a restaurant kitchen filling orders:
- Several tables place orders (tasks) at different times
- The chef prepares all orders simultaneously
- As each dish is completed, the waiter immediately serves it to the appropriate table
- Tables that ordered simpler dishes get served first, regardless of when they ordered
You don’t wait for all tables to be served before delivering any food - each is served as soon as it’s ready.
Differences From gather()
- Order:
gather()
returns results in the input order;as_completed()
returns in completion order - Processing:
gather()
waits for all results;as_completed()
allows processing as they arrive - Result format:
gather()
returns a list;as_completed()
yields individual results
Practical Use Cases
as_completed()
is ideal when:
- You want to display results to users as they become available
- Some tasks might take much longer than others
- You need to process each result independently
- You’re fetching many items and want to start working with the first ones immediately
|
|
By processing results as they arrive, your application can feel much more responsive and efficient, especially when dealing with operations that have widely varying completion times.