Skip to content

Week 14 - Structured Concurrency, Cancellation, ExceptionGroups, anyio

14.1 Conceptual Core

  • Structured concurrency: a parent task does not exit until its children have finished. No orphaned tasks, no leaked work. asyncio.TaskGroup (3.11+) and anyio.create_task_group are the canonical implementations; both inspired by Trio.
  • Cancellation must be honored. A coroutine that catches BaseException (or worse, Exception in <3.11) and ignores it breaks structured concurrency. The contract: if you must catch, re-raise CancelledError.
  • ExceptionGroup (PEP 654, 3.11+): when multiple sibling tasks fail, you get an ExceptionGroup containing all of them. except* ValueError: matches the subset.

14.2 Mechanical Detail

  • anyio as the portable abstraction: works on asyncio or trio backends, gives you create_task_group, move_on_after, fail_after, to_thread.run_sync, from_thread.run. If you write libraries, prefer anyio over raw asyncio.
  • contextvars.ContextVar: the async-safe replacement for threading.local. Used by tracing libraries, request-id propagation, FastAPI dependencies.
  • Backpressure: bounded asyncio.Queue is your friend. Unbounded queues are how async services OOM in production.
  • The eager_task_factory (3.12+): start tasks eagerly when possible, reducing scheduling overhead.

14.3 Lab - "The Fan-Out That Cleans Up After Itself"

  1. Refactor your week-13 crawler to use TaskGroup (or anyio task group).
  2. Add a "first-error wins" mode: as soon as any task raises, all siblings are cancelled and the group raises an ExceptionGroup.
  3. Add a "best-effort" mode: collect all results and exceptions, return both.
  4. Verify via test that cancelling the parent cancels every in-flight HTTP request within 100ms.

14.4 Idiomatic & Linter Drill

  • Add ruff RUF006 (asyncio dangling tasks). Refactor any create_task not held in a TaskGroup or kept-reference set.

14.5 Production Hardening Slice

  • Add OpenTelemetry instrumentation. Verify trace context propagates across TaskGroup boundaries.

Comments