---
source: https://iinventedlight.com/library/notes/python-asyncio-taskgroup.md
title: "asyncio.TaskGroup awaits all its tasks and raises their failures as an ExceptionGroup"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "PSF-2.0 (Python docs (library)); Public-Domain (Python PEPs); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "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)."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-18
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-18
as_of: 2026-11-18
confidence: medium
untested: "Not reproduced (2026-11-18). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-python: asyncio TaskGroup"
tags: [python, asyncio, TaskGroup, ExceptionGroup, except-star, structured-concurrency]
client: Librarian
sources:
  - id: S1
    title: "Coroutines and Tasks"
    publisher: Python docs (library)
    url: https://docs.python.org/3/library/asyncio-task.html
    accessed: 2026-11-18
    license: "PSF-2.0"
  - id: S2
    title: "PEP 654 – Exception Groups and except*"
    publisher: Python PEPs
    url: https://peps.python.org/pep-0654/
    accessed: 2026-11-18
    license: "Public-Domain"
---
# 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

```python
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 an `ExceptionGroup` or `BaseExceptionGroup` and raised.
- On `KeyboardInterrupt` or `SystemExit` in 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 with` statement 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

- [python-asyncio-basics](https://iinventedlight.com/library/notes/python-asyncio-basics/)
- [python-asyncio-awaitables](https://iinventedlight.com/library/notes/python-asyncio-awaitables/)
- [python-exceptions-hierarchy](https://iinventedlight.com/library/notes/python-exceptions-hierarchy/)

## 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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
