Skip to content

Module 06: Structured Concurrency

← Module 05 | Topic Home | Next → Module 07


Status Difficulty Time

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

  1. Overview
  2. Prerequisites
  3. Objectives
  4. Theory
  5. Key Concepts
  6. Examples
  7. Common Pitfalls
  8. Cross-Links
  9. Summary

Prerequisites

  • Module 05: Synchronization Primitives
  • Module 03: Tasks and Futures (especially task cancellation)
  • Python 3.11+ for TaskGroup (or anyio for backport)

Objectives

By the end of this module, you will be able to:

  1. Explain the problem that structured concurrency solves
  2. Use asyncio.TaskGroup to manage multiple concurrent tasks with automatic cleanup
  3. Handle ExceptionGroup with except* syntax
  4. Understand how TaskGroup propagates errors and cancels sibling tasks
  5. Use anyio.create_task_group() as an asyncio-compatible alternative that also works on Trio
  6. Implement cancellation scopes using asyncio.timeout() and anyio.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 cancellation
  • ExceptionGroup and except*: handling multiple exceptions from a task group
  • PEP 654: why exception groups were needed
  • asyncio.timeout(): context manager for timeouts (Python 3.11+) vs wait_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.


  • [[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.