Module 06: Structured Concurrency¶
← Module 05 | Topic Home | Next → Module 07
Stub module — Full content to be generated. This file defines the scope and prerequisites so the topic roadmap is complete.
Overview¶
Unstructured concurrency — creating tasks with create_task() and hoping they complete
— is a recipe for resource leaks, silent failures, and orphaned tasks. Structured
concurrency is the discipline of bounding the lifetime of concurrent tasks to the
lexical scope in which they were created: when the scope exits, all tasks are guaranteed
to be complete (or cancelled).
Python 3.11 introduced asyncio.TaskGroup as the standard structured concurrency
primitive. This module covers TaskGroup, exception handling with ExceptionGroup, the
anyio library for writing async code that works with multiple event loop backends,
and cancellation scope patterns.
Table of Contents¶
Prerequisites¶
- Module 05: Synchronization Primitives
- Module 03: Tasks and Futures (especially task cancellation)
- Python 3.11+ for
TaskGroup(oranyiofor backport)
Objectives¶
By the end of this module, you will be able to:
- Explain the problem that structured concurrency solves
- Use
asyncio.TaskGroupto manage multiple concurrent tasks with automatic cleanup - Handle
ExceptionGroupwithexcept*syntax - Understand how
TaskGrouppropagates errors and cancels sibling tasks - Use
anyio.create_task_group()as an asyncio-compatible alternative that also works on Trio - Implement cancellation scopes using
asyncio.timeout()andanyio.move_on_after()
Theory¶
Full content to be generated. Topics to cover:
- The problem with
create_task(): tasks outlive their creator, errors are lost- Nurseries in Trio: the origin of the pattern
asyncio.TaskGroup(Python 3.11+): usage, lifecycle, automatic cancellationExceptionGroupandexcept*: handling multiple exceptions from a task group- PEP 654: why exception groups were needed
asyncio.timeout(): context manager for timeouts (Python 3.11+) vswait_for()anyio:create_task_group(),move_on_after(),fail_after(),CancelScope- When to use anyio vs. native asyncio: library code vs. application code
Key Concepts¶
To be filled in when this module is fully generated.
Examples¶
To be filled in when this module is fully generated.
Common Pitfalls¶
To be filled in when this module is fully generated.
Cross-Links¶
- [[async-python/modules/05_synchronization]] — prerequisite
- [[async-python/modules/10_production-patterns]] — graceful shutdown patterns use structured concurrency heavily
- [[async-python/modules/11_capstone-project]] — the capstone uses TaskGroup throughout
Summary¶
To be filled in when this module is fully generated.