Process faster tasks first using `as_completed()`.
Section: Tasks and Concurrency
Consume results as tasks finish
python
python
import asyncio
async def worker(name, delay):
await asyncio.sleep(delay)
return name
async def main():
tasks = [worker("a", 0.3), worker("b", 0.1), worker("c", 0.2)]
for future in asyncio.as_completed(tasks):
print(await future)
asyncio.run(main())Explanation
Useful when you want streaming-like processing of concurrent results.
Learn the surrounding workflow
Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.
Related commands
Same sheet · prioritizing Tasks and Concurrency
Run tasks concurrently with gather
Wait for multiple coroutines and collect results.
Collect exceptions with gather
Keep other tasks running even if one fails.
Use TaskGroup for structured concurrency
Run related tasks under a context manager.
Wait until the first task completes
Use `asyncio.wait()` to control waiting behavior.
Run an async entrypoint
Execute a top-level coroutine and manage the event loop automatically.