← All notes

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

except*

PEP 654 specifies ExceptionGroup, BaseExceptionGroup and handling exception groups with except*.

See also

Claims

Sources

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

Permanent ID: iil:note:xet72z6f

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.},
}

Markdown

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.