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
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.
License: CC-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.
Cite this
iinventedlight. 2026. "Use Swift task groups for structured concurrency." iinventedlight Library, iil:note:v2ebgsje, version of 2026-11-19. https://iinventedlight.com/library/notes/swift-structured-concurrency/. License: CC-BY-4.0.
@misc{iil_note_v2ebgsje,
author = {{iinventedlight}},
title = {{Use Swift task groups for structured concurrency}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-19},
url = {https://iinventedlight.com/library/notes/swift-structured-concurrency/},
note = {Permanent ID iil:note:v2ebgsje. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- swift-structured-concurrency.table.json (table; CC-BY-4.0)
- swift-structured-concurrency.schema.json (schema; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/swift-structured-concurrency.md.