scope
fn scope<T>(Scope -> T body) -> Result<T, TaskError> Import
Source: use task::{scope}; compiler/src/prelude/task.hy Runs body with a Scope for starting child tasks, and returns its value once body and every child have finished.
Tasks run on the current thread and share the heap; they take turns at suspension points, so ordinary IO functions overlap without callbacks. This is structured concurrency: no task outlives its scope.
Parameters#
| Name | Type |
|---|---|
body | Scope -> T |
Returns#
Result<T, TaskError>
Errors#
When a child panics, the scope cancels its other children, waits for them
and returns Err(TaskError::Panicked(message)).
Panics#
Panics with task deadlock when every task is waiting on another one.
Examples#
use task::{scope, sleep, Scope};
use tests::assert;
let order = scope(fn (Scope s) {
let slow = s.spawn(fn () {
sleep(20);
return "slow";
});
let fast = s.spawn(fn () => "fast");
return (fast.join() ?? "") + "," + (slow.join() ?? "");
});
assert!((order ?? "") == "fast,slow");
See also#
task::Scopesleep: Suspends the current task for at leastmsmilliseconds; other tasks run meanwhile.yield_now: Lets other ready tasks run before the current one continues.timeout: Runsbodyas a task and cancels it if it has not finished aftermsmilliseconds.shield: Runsbodyso that a cancel waits for it to finish instead of interrupting it.channel: Creates a channel that holds up tocapacityvalues.blocking: Runsworkon another OS thread and waits for its result; only the current task waits, the others keep running.blocking_with: Likeblocking, passingargtowork(it is copied to the other thread, likethread::spawn(work, arg)).