asyncio.TaskGroup awaits all its tasks and raises their failures as an ExceptionGroup
Answer: asyncio.TaskGroup is an asynchronous context manager that holds a group of tasks. Add tasks to the group with create_task(); all tasks are awaited on exit. If tasks fail, their exceptions are combined in an ExceptionGroup, which you handle with except*.
Example
import asyncio
async def main():
try:
async with asyncio.TaskGroup() as tg:
t1 = tg.create_task(fetch("a"))
t2 = tg.create_task(fetch("b"))
print(t1.result(), t2.result())
except* TimeoutError as eg:
print(len(eg.exceptions), "timeouts")
Failure semantics
- After all tasks have finished, exceptions from failed tasks (other than
asyncio.CancelledError) are combined in anExceptionGrouporBaseExceptionGroupand raised. - On
KeyboardInterruptorSystemExitin a task, the group still cancels the remaining tasks and waits for them, then re-raises that exception instead of a group. - If the body of the
async withstatement exits with an exception, this is treated like a failed task: the remaining tasks are cancelled.
except*
PEP 654 specifies ExceptionGroup, BaseExceptionGroup and handling exception groups with except*.
See also
Claims
- id: C1 claim: TaskGroup is an asynchronous context manager holding a group of tasks. source: S1 evidence: "An asynchronous context manager holding a group of tasks."
- id: C2 claim: Failed task exceptions are combined in an ExceptionGroup. source: S1 evidence: "those exceptions are combined in an ExceptionGroup or BaseExceptionGroup"
- id: C3 claim: Tasks are added with create_task. source: S1 evidence: "Tasks can be added to the group using create_task() ."
- id: C4 claim: PEP 654 introduced exception groups and except. source: S2 evidence: "Exception Groups and except"
Sources
- [S1] Coroutines and Tasks. Python docs (library). https://docs.python.org/3/library/asyncio-task.html . License: PSF-2.0. Accessed 2026-11-18.
- [S2] PEP 654 – Exception Groups and except*. Python PEPs. https://peps.python.org/pep-0654/ . License: Public-Domain. Accessed 2026-11-18.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python docs (library), Coroutines and Tasks; Python PEPs, PEP 654 – Exception Groups and except*. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation. Public-Domain (https://peps.python.org/pep-0001/). PEP text is placed in the public domain (some also CC0-1.0). Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Python docs (library), Coroutines and Tasks; Python PEPs, PEP 654 – Exception Groups and except*. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation. Public-Domain (https://peps.python.org/pep-0001/). PEP text is placed in the public domain (some also CC0-1.0).
Cite this
iinventedlight. 2026. "asyncio.TaskGroup awaits all its tasks and raises their failures as an ExceptionGroup." iinventedlight Library, iil:note:xet72z6f, version of 2026-11-18. https://iinventedlight.com/library/notes/python-asyncio-taskgroup/. License: CC-BY-4.0.
@misc{iil_note_xet72z6f,
author = {{iinventedlight}},
title = {{asyncio.TaskGroup awaits all its tasks and raises their failures as an ExceptionGroup}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-18},
url = {https://iinventedlight.com/library/notes/python-asyncio-taskgroup/},
note = {Permanent ID iil:note:xet72z6f. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/python-asyncio-taskgroup.md.