IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /docs/manual/basics.md). For the complete Mojo documentation index, see llms.txt.
Skip to main content
Version: 1.0.0
For the complete Mojo documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /docs/manual/basics.md).

TaskGroup

struct TaskGroup

A group of tasks that can be executed concurrently.

TaskGroup manages a collection of coroutines that can be executed in parallel. It provides mechanisms to create, track, and wait for the completion of tasks.

Fields​

  • ​counter (Atomic[DType.int]): Atomic counter tracking the number of active tasks in the group.
  • ​chain (_Chain): Chain used for asynchronous completion notification.
  • ​tasks (List[_TaskGroupBox]): Collection of tasks managed by this TaskGroup.

Implemented traits​

AnyType, Defaultable, Deinitable, Movable

Methods​

__init__​

def __init__(out self)

Initialize a new TaskGroup with an empty task list and initialized chain.

__deinit__​

def __deinit__(deinit self)

Clean up resources associated with the TaskGroup.

__await__​

def __await__(mut self)

Make TaskGroup awaitable in async contexts.

This allows using 'await task_group' syntax in async functions.

create_task​

def create_task(mut self, var task: Coroutine[None])

Add a new task to the TaskGroup for execution.

Args:

await_body_impl​

static def await_body_impl(hdl: AnyCoroutine, mut task_group: Self)

Implementation of the await functionality for TaskGroup.

Args:

  • ​hdl (AnyCoroutine): The coroutine handle to be awaited.
  • ​task_group (Self): The TaskGroup to be awaited.

wait​

def wait[origins: OriginSet = {}](mut self)

Wait for all tasks in the TaskGroup to complete.

This is a blocking call that returns only when all tasks have finished.

Parameters:

  • ​origins (OriginSet): The origin set for the wait operation.