timeout
fn timeout<T>(int ms, unit -> T body) -> Result<T, TaskError> Import
Source: use task::{timeout}; compiler/src/prelude/task.hy Runs body as a task and cancels it if it has not finished after ms milliseconds.
Parameters#
| Name | Type |
|---|---|
ms | int |
body | unit -> T |
Returns#
Result<T, TaskError>
Errors#
Err(TaskError::TimedOut) when the deadline passed first, and
Err(TaskError::Panicked(message)) when body panicked.
Examples#
use task::{timeout, sleep, TaskError};
use tests::assert;
let r = timeout(10, fn () {
sleep(1000);
return 1;
});
let timed_out = match r {
Result::Err(TaskError::TimedOut) => true,
default => false,
};
assert!(timed_out);
assert!((timeout(1000, fn () => 7) ?? 0) == 7);
See also#
scope: Runsbodywith aScopefor starting child tasks, and returns its value oncebodyand every child have finished.sleep: Suspends the current task for at leastmsmilliseconds; other tasks run meanwhile.yield_now: Lets other ready tasks run before the current one continues.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)).block_on: Resumes the coroutinehandleuntil it is done and returns its completion value; values it yields are discarded.