Middle
When to use asyncio.gather, and when TaskGroup? Pros and cons of each.
sobes.tech AI
Answer from AI
asyncio.gather and asyncio.TaskGroup are both used to run multiple coroutines concurrently but have different scenarios and features.
asyncio.gather:
- Runs multiple coroutines and collects their results.
- If one coroutine raises an exception, gather by default cancels the others and raises the exception.
- Can be used with the parameter
return_exceptions=Trueto collect all exceptions as results. - Suitable when you need to get results from all tasks and process them together.
asyncio.TaskGroup (Python 3.11+):
- A context manager for grouping tasks.
- Allows creating tasks within a block, automatically waiting for their completion.
- Cancels remaining tasks if one task raises an exception.
- More convenient for structuring code and managing task lifecycle.
Pros and cons:
| Feature | asyncio.gather | asyncio.TaskGroup |
|---|---|---|
| Python version | 3.7+ | 3.11+ |
| Task management | Explicit creation and collection | Automatic management |
| Exception handling | Can collect all exceptions | Cancels all on exception |
| Code readability | Less structured | More structured code |
When to use:
- If compatibility with Python versions below 3.11 is needed —
asyncio.gather. - If structured code and automatic task management are important —
TaskGroup.
Example with TaskGroup:
import asyncio
async def worker(n):
await asyncio.sleep(n)
return n
async def main():
async with asyncio.TaskGroup() as tg:
tg.create_task(worker(1))
tg.create_task(worker(2))
print("All tasks completed")
asyncio.run(main())