LEARN VOID / CONCURRENCY
Task groups & cancellation
Give related work a lifetime.
Complete program for the explicit async host adapter. The result below is the returned integer.
async func line_count(pages: i32) -> i32:
return pages * 40
async func entry(chapter_pages: i32) -> i32:
var total_lines = 0
with tasks.group() as chapters:
var chapter = line_count(chapter_pages)
var appendix = line_count(1)
total_lines = await chapter + await appendix
return total_linesGive chapter work one scope
The group owns the two line_count tasks created inside it. Each uses a fixed forty-lines-per-page model, so the chapter and one-page appendix have independent integer results. No shared counter is mutated.
Keep the result outside the group
Declare total_lines outside the group so it is available afterward. The parent explicitly awaits both results; normal scope exit also joins any remaining children. Input 5 gives 200 + 40 = 240. The group alias is lexical, not a first-class collection or a jobs.spawn API.
Understand early exits
Early exits from nested scopes request cooperative cancellation of unfinished work and join it before leaving. Dropping a handle never detaches a task. This example shows successful completion through the explicit async host adapter; it does not demonstrate cancellation recovery or change the ordinary CLI adapter.