Python Structured Concurrency Patterns — ELI5

Imagine a school field trip. The teacher takes 30 kids to a museum. There are two ways to handle this:

The chaotic way: Tell the kids “go explore, come back to the bus whenever.” Some kids get lost. One kid is still in the gift shop when the bus leaves. Another kid fell asleep in the dinosaur exhibit. The teacher has no idea where anyone is.

The structured way: Split into groups. Each group has a chaperone. The chaperone knows exactly which kids are in their group. Nobody leaves the museum until every kid in the group is accounted for. If one kid gets hurt, the whole group stops and deals with it.

Structured concurrency is the second approach, but for your code’s tasks.

In regular async code, you can create tasks that fly off and do their own thing. Nobody tracks them. If one fails, the others don’t notice. If your program tries to shut down, some tasks might still be running in the background.

With structured concurrency, tasks belong to a group. The group doesn’t finish until all its tasks finish. If one task crashes, the group cancels the others and reports the error. No orphans. No silent failures.

Python added this with TaskGroup in version 3.11:

async with asyncio.TaskGroup() as group:
    group.create_task(download_file("a.txt"))
    group.create_task(download_file("b.txt"))
# Both tasks are guaranteed to be done here

When the code exits that block, both downloads are either complete or cancelled. Nothing leaks out.

One thing to remember: Structured concurrency is like a field trip with a chaperone — every task belongs to a group, and the group doesn’t finish until every task is accounted for.

pythonconcurrencyasynciostructured-concurrency

See Also

  • Python Actor Model Why treating each piece of your program like a person with their own mailbox makes concurrency way less scary.
  • Python Aiocache Caching aiocache remembers expensive answers so your async Python app doesn't waste time asking the same question twice.
  • Python Aiofiles Async Io aiofiles lets your async Python program read and write files without freezing — because normal file operations secretly block everything.
  • Python Aiohttp Understand Aiohttp through an everyday analogy so Python behavior feels intuitive, not random.
  • Python Anyio Portability AnyIO lets your async Python code work with any async library — write once, run on asyncio or Trio without changes.