coroutine

Module

Implements classes and methods for coroutines.

These are Mojo built-ins, so you don’t need to import them.

CoroutineContext

The default context for a Coroutine, capturing the resume function callback and parent Coroutine. The resume function will typically just resume the parent. May be overwritten by other context types with different interpretations of the payload, but which nevertheless be the same size and contain the resume function and a payload pointer.

Implemented traits:

AnyType

Coroutine

Represents a coroutine.

Coroutines can pause execution saving the state of the program (including values of local variables and the location of the next instruction to be executed). When the coroutine is resumed, execution continues from where it left off, with the saved state restored.

Parameters:

  • type (AnyRegType): Type of value returned upon completion of the coroutine.

Implemented traits:

AnyType

Methods:

__init__

__init__(handle: !pop.coroutine<() -> !kgen.paramref<*"type">>) -> Self

Construct a coroutine object from a handle.

Args:

  • handle (!pop.coroutine<() -> !kgen.paramref<*"type">>): The init handle.

Returns:

The constructed coroutine object.

__del__

__del__(owned self: Self)

Destroy the coroutine object.

__await__

__await__(self: Self) -> *"type"

Suspends the current coroutine until the coroutine is complete.

Returns:

The coroutine promise.

get_promise

get_promise(self: Self) -> Pointer[*"type", 0]

Return the pointer to the beginning of the memory where the async function results are stored.

Returns:

The coroutine promise.

get

get(self: Self) -> *"type"

Get the value of the fulfilled coroutine promise.

Returns:

The value of the fulfilled promise.

get_ctx

get_ctx[ctx_type: AnyRegType](self: Self) -> Pointer[ctx_type, 0]

Returns the pointer to the coroutine context.

Parameters:

  • ctx_type (AnyRegType): The type of the coroutine context.

Returns:

The coroutine context.

__call__

__call__(self: Self) -> *"type"

Execute the coroutine synchronously.

Returns:

The coroutine promise.

RaisingCoroutine

Represents a coroutine that can raise.

Coroutines can pause execution saving the state of the program (including values of local variables and the location of the next instruction to be executed). When the coroutine is resumed, execution continues from where it left off, with the saved state restored.

Parameters:

  • type (AnyRegType): Type of value returned upon completion of the coroutine.

Implemented traits:

AnyType

Methods:

__init__

__init__(handle: !pop.coroutine<() throws -> !kgen.variant<!kgen.declref<_"$stdlib"::_"$builtin"::_"$error"::_Error, !lit.metatype<_"$stdlib"::_"$builtin"::_"$error"::_Error>>, *"type">>) -> Self

Construct a coroutine object from a handle.

Args:

  • handle (!pop.coroutine<() throws -> !kgen.variant<!kgen.declref<_"$stdlib"::_"$builtin"::_"$error"::_Error, !lit.metatype<_"$stdlib"::_"$builtin"::_"$error"::_Error>>, *"type">>): The init handle.

Returns:

The constructed coroutine object.

__del__

__del__(owned self: Self)

Destroy the coroutine object.

__await__

__await__(self: Self) -> *"type"

Suspends the current coroutine until the coroutine is complete.

Returns:

The coroutine promise.

get_promise

get_promise(self: Self) -> Pointer[Variant[Error, *"type"], 0]

Return the pointer to the beginning of the memory where the async function results are stored.

Returns:

The coroutine promise.

get

get(self: Self) -> *"type"

Get the value of the fulfilled coroutine promise.

Returns:

The value of the fulfilled promise.

get_ctx

get_ctx[ctx_type: AnyRegType](self: Self) -> Pointer[ctx_type, 0]

Returns the pointer to the coroutine context.

Parameters:

  • ctx_type (AnyRegType): The type of the coroutine context.

Returns:

The coroutine context.

__call__

__call__(self: Self) -> *"type"

Execute the coroutine synchronously.

Returns:

The coroutine promise.