---
source: https://iinventedlight.com/library/notes/swift-structured-concurrency.md
title: "Use Swift task groups for structured concurrency"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "Apache-2.0 (The Swift Programming Language); PSF-2.0 (Python docs); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "Summary by iinventedlight (AI-assisted). Sources: The Swift Programming Language, Concurrency; Python docs, Coroutines and Tasks. Apache-2.0; PSF-2.0."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-19
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-19
as_of: 2026-11-19
confidence: medium
untested: "Not reproduced (2026-11-19). 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-systems: Swift structured concurrency and TaskGroup"
tags: [swift, concurrency, TaskGroup, withTaskGroup, async-let, structured-concurrency]
client: Librarian
sources:
  - id: S1
    title: "Concurrency"
    publisher: The Swift Programming Language
    url: https://raw.githubusercontent.com/swiftlang/swift-book/main/TSPL.docc/LanguageGuide/Concurrency.md
    accessed: 2026-11-19
    license: "Apache-2.0"
  - id: S2
    title: "Coroutines and Tasks"
    publisher: Python docs
    url: https://docs.python.org/3/library/asyncio-task.html
    accessed: 2026-11-19
    license: "PSF-2.0"
---
# Use Swift task groups for structured concurrency
**Answer:** In Swift, tasks form a hierarchy. A parent task cannot forget to wait for its child tasks to complete. That parent-child relationship is called structured concurrency. Create child tasks with `withTaskGroup`. `TaskGroup` in asyncio is a task creation API with a convenient way to wait for all tasks in the group to finish.

## Task and async-let

All asynchronous code runs as part of some task. A task does one thing at a time. After you create multiple tasks, Swift can schedule them to run simultaneously. The `async let` syntax implicitly creates a child task.

## Task groups

You can also create a `TaskGroup` and add child tasks to it explicitly. That gives you more control over priority and cancellation, and you can create a dynamic number of tasks. The docs show `await withTaskGroup(of: Data.self)` and, inside the group, `group.addTask`. Results can be taken with `for await photo in group`.

| Wait for child tasks | Priority |
|---|---|
| Wait for child tasks | In a parent task, you cannot forget to wait for its child tasks to complete. |
| Priority | When setting a higher priority on a child task, the parent task priority is escalated automatically. |
| Cancellation | When a parent task is canceled, each child task is also automatically canceled. |
| Task-local values | Task-local values propagate to child tasks efficiently and automatically. |

Swift detects and prevents data races. Most of those races produce a compile-time error.

## Compare with asyncio.TaskGroup

`asyncio.TaskGroup` is a more modern alternative to `create_task()`. The `async with` statement will wait for all tasks in the group to finish. New tasks may still be added while that wait is in progress. If tasks raise, those exceptions are combined in an `ExceptionGroup` or `BaseExceptionGroup`.

## See also

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

## Claims

- id: C1
  claim: Swift calls the parent-child task relationship structured concurrency.
  source: S1
  evidence: "this approach is called *structured concurrency*"

- id: C3
  claim: Canceling a parent task also cancels its child tasks.
  source: S1
  evidence: "each of its child tasks is also automatically canceled"

- id: C4
  claim: asyncio.TaskGroup waits until every task in the group finishes.
  source: S2
  evidence: "will wait for all tasks in the group to finish"

## Sources

- [S1] Concurrency. The Swift Programming Language. https://raw.githubusercontent.com/swiftlang/swift-book/main/TSPL.docc/LanguageGuide/Concurrency.md . License: Apache-2.0. Accessed 2026-11-19.

- [S2] Coroutines and Tasks. Python docs. https://docs.python.org/3/library/asyncio-task.html . License: PSF-2.0. Accessed 2026-11-19.

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: The Swift Programming Language, Concurrency; Python docs, Coroutines and Tasks. Apache-2.0; PSF-2.0. Text was condensed and changed from the originals.

---

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