Under The Hood
backend python CPython 3.11+

Asyncio TaskGroup, ExceptionGroups, and Cooperative Cancel Scopes

Last updated
Prerequisites:
Python async/await coroutines
Asyncio event loop basics
  • python
  • asyncio
  • taskgroup
  • structured-concurrency
  • exceptiongroup
  • cancellation

Read at your depth

The practical view

`asyncio.TaskGroup` (introduced in Python 3.11) brings **Structured Concurrency** to Python, replacing `asyncio.gather()` and raw `asyncio.create_task()`. Using `async with asyncio.TaskGroup() as tg:`, you spawn tasks with `tg.create_task(coro())`. If any child task raises an exception, the TaskGroup automatically cancels all remaining running sibling tasks and groups all errors into an `ExceptionGroup` (handled using `except*`).

The same idea in other frameworks

python equivalent
`async with asyncio.TaskGroup() as tg: tg.create_task(...)` with `except*`.

Legacy vs modern

asyncio.gather Orphan Leaks vs Structured asyncio.TaskGroup

`asyncio.gather` leaves sibling tasks running when one fails; `TaskGroup` cleanly cancels siblings and aggregates exceptions.

before → after
asyncio.gather (Orphaned Task Leak)
await asyncio.gather(fetch_a(), fetch_b()); // ❌ If B fails, A continues running orphaned in background
Structured asyncio.TaskGroup
async with asyncio.TaskGroup() as tg: tg.create_task(a()); tg.create_task(b()); // ✅ NEW: Auto-cancels siblings

Interview gotchas

Context

Interviewers test your knowledge of Python 3.11+ exception group pattern matching.

The mechanical answer

Standard `except ValueError` cannot match inside an `ExceptionGroup` containing multiple disparate errors. **`except* ValueError as eg`** matches and extracts all matching `ValueError` exceptions from the group, allowing other handlers (e.g. `except* TypeError`) in the same `try/except*` block to simultaneously match and handle their respective errors from the same composite exception tree.

Trap

You cannot mix traditional `except` and modern `except*` in the same `try` statement.

Under The Hood — a multi-depth technical interview hub.

Third-party licenses Press ⌘ K to search.