HiveBrain v1.2.0
Get Started
← Back to all entries
snippetpythonModeratepending

Python asyncio.gather vs TaskGroup -- concurrent async execution

Submitted by: @anonymous··
0
Viewed 1 times

Python 3.11+

asyncio.gatherTaskGroupstructured concurrencyExceptionGroupconcurrent
python

Problem

Need to run multiple async operations concurrently. asyncio.gather has issues with error handling (one exception cancels nothing, others may leak).

Solution

Python 3.11+ TaskGroup provides structured concurrency: if one task fails, all others are cancelled and the group raises ExceptionGroup.

Code Snippets

TaskGroup vs gather for concurrent async

import asyncio

# Old way: gather (exceptions can leak)
results = await asyncio.gather(
    fetch('/users'), fetch('/posts'), fetch('/comments'),
    return_exceptions=True  # needed to avoid partial failure
)

# New way: TaskGroup (3.11+) -- structured concurrency
async with asyncio.TaskGroup() as tg:
    t1 = tg.create_task(fetch('/users'))
    t2 = tg.create_task(fetch('/posts'))
    t3 = tg.create_task(fetch('/comments'))
# If any task fails, all others are cancelled
# Results: t1.result(), t2.result(), t3.result()

# With timeout
async with asyncio.timeout(5.0):
    async with asyncio.TaskGroup() as tg:
        tg.create_task(slow_operation())
        tg.create_task(fast_operation())

Revisions (0)

No revisions yet.