gleam/otp/task
A task is a kind of process that computes a value and then sends the result back to its parent. Commonly multiple tasks are used to compute multiple things at once.
If you do not care to receive a result back at the end then you should not
use this module, actor
or process
are likely more suitable.
let task = task.async(fn() { do_some_work() })
let value = do_some_other_work()
value + task.await(task, 100)
Tasks spawned with async can be awaited on by their caller process (and only their caller) as shown in the example above. They are implemented by spawning a process that sends a message to the caller once the given computation is performed.
There are some important things to consider when using tasks:
-
If you are using async tasks, you must await a reply as they are always sent.
-
Tasks link the caller and the spawned process. This means that, if the caller crashes, the task will crash too and vice-versa. This is on purpose: if the process meant to receive the result no longer exists, there is no purpose in completing the computation.
-
A task’s callback function must complete by returning or panicking. It must not
exit
with the reason “normal”.
This module is inspired by Elixir’s Task module.
Types
pub type AwaitError {
Timeout
Exit(reason: Dynamic)
}
Constructors
-
Timeout
-
Exit(reason: Dynamic)
Functions
pub fn async(work: fn() -> a) -> Task(a)
Spawn a task process that calls a given function in order to perform some
work. The result of this function is sent back to the parent and can be
received using the await
function.
See the top level module documentation for more information on async/await.
pub fn await(task: Task(a), timeout: Int) -> a
Wait for the value computed by a task.
If the a value is not received before the timeout has elapsed or if the task process crashes then this function crashes.
pub fn await_forever(task: Task(a)) -> a
Wait endlessly for the value computed by a task.
Be Careful! Like try_await_forever
, this function does not return until
there is a value to receive.
If the task process crashes then this function crashes.
pub fn try_await(
task: Task(a),
timeout: Int,
) -> Result(a, AwaitError)
Wait for the value computed by a task.
If the a value is not received before the timeout has elapsed then an error is returned.
pub fn try_await2(
task1: Task(a),
task2: Task(b),
timeout: Int,
) -> #(Result(a, AwaitError), Result(b, AwaitError))
Wait for the values computed by multiple tasks.
For each task, if the a value is not received before the timeout has elapsed then an error is returned.
pub fn try_await3(
task1: Task(a),
task2: Task(b),
task3: Task(c),
timeout: Int,
) -> #(
Result(a, AwaitError),
Result(b, AwaitError),
Result(c, AwaitError),
)
Wait for the values computed by multiple tasks.
For each task, if the a value is not received before the timeout has elapsed then an error is returned.
pub fn try_await4(
task1: Task(a),
task2: Task(b),
task3: Task(c),
task4: Task(d),
timeout: Int,
) -> #(
Result(a, AwaitError),
Result(b, AwaitError),
Result(c, AwaitError),
Result(d, AwaitError),
)
Wait for the values computed by multiple tasks.
For each task, if the a value is not received before the timeout has elapsed then an error is returned.
pub fn try_await_all(
tasks: List(Task(a)),
timeout: Int,
) -> List(Result(a, AwaitError))
Wait for the values computed by multiple tasks.
For each task, if the a value is not received before the timeout has elapsed then an error is returned.