9.3. Async/await coroutine macros¶
The ASYNC_BOOST module implements an async/await pattern for daslang using
generator-based cooperative multitasking. It provides the [async] function
annotation, await for waiting on results, and await_next_frame for
suspending until the next step. Under the hood every [async] function is
transformed into a state-machine generator — no threads, channels, or job
queues are involved.
All functions and symbols are in “async_boost” module, use require to get access to it.
require daslib/async_boost
See also
tutorial_async — Tutorial 49: Async / Await.
Coroutines and additional generator support — coroutines module (underlying generator framework).
9.3.1. Function annotations¶
- AwaitMacro¶
Function annotation that implements coroutine await semantics.
- AwaitCoroutineMacro¶
This macro converts await(<coroutine>) expression into:
for t in THAT
yield t
The idea is that coroutine or generator can continuously yield from another sub-coroutine or generator.
- async¶
This macro converts function into generator. Generator yields bool if its a void function (coroutine), and yields the return type otherwise (async return). async function can wait for another async function using await(<async fn call>). use ‘return false’ to immediately return from the generator.
9.3.2. Awaiting¶
9.3.2.1. await¶
- await(a: iterator<bool>): bool¶
This function is used to wait for the result of the async function.
- Arguments:
a : iterator<bool>
- await(a: iterator<variant<res:auto(T);wait:bool>>): T
- await_next_frame()¶
This function is used to suspend coroutine until next frame.
9.3.3. Running async tasks¶
- async_race(a: iterator<auto>; b: iterator<auto>): int¶
This function runs two async functions concurrently and returns the index (0 or 1) of whichever finishes first. The other is abandoned.
- Arguments:
a : iterator<auto>
b : iterator<auto>
- async_run(a: iterator<auto>): auto¶
This function runs async function until it is finished.
- Arguments:
a : iterator<auto>
- async_run_all(a: array<iterator<auto>>): auto¶
This function runs all async function until they are finished (in parallel, starting from the last one).
- Arguments:
a : array<iterator<auto>>
- async_timeout(a: iterator<auto>; max_frames: int): bool¶
This function runs an async function for at most max_frames frames.
Returns true if the async function completed within the limit,
false if it was terminated due to timeout.
- Arguments:
a : iterator<auto>
max_frames : int