Callable execution in a new context with a cancellation deadline.
runFor
(callable out duration -- output timedOut) Runs callable in a new context under a deadline and returns its output and whether the deadline requested cancellation.callableandoutare passed tospawn, whose rules for them apply. With()asout,runForreturnstimedOutalone.outputis made bynewfrom the result ofgeton the context ofcallable, soTextandCodeare not supported output schemas here; return an ownedStringinstead ofText.durationis theReal64number of seconds from the call ofrunForuntil the deadline and must be positive. Checked violations reportinvalid duration; a known violation can fail compilation. Run-time assertions requireDEBUG.- A timer context requests cancellation of the context of
callableif that context is still unfinished when the timer runs, at the deadline or later;timedOutisTRUEif the timer made the request andFALSEotherwise. - After
callablehas started, the scheduler checks the deadline only while no context is running and, on Linux and macOS, none is ready; a context that yields stays ready. - The cancellation does not terminate
callable, which can observe it withcanceled?:runForreturns only aftercallablehas returned, andoutputis its output also whentimedOutisTRUE. - If the calling context is already canceled,
spawnis not called:timedOutisFALSE, andoutputis made fromoutbynew. runForwaits withget, so canceling the calling context meanwhile also requests cancellation of the context ofcallable;timedOutstill reports the timer's request alone.
Examples
Example: completion and timeout
The first callable returns before its deadline; the timer cancels the second, cutting its sleep short.
"String" use
"control" use
"sync/runFor" use
"sync/sync" use
{} Int32 {} [
fast: fastTimedOut: [42] Int32 1.0r64 runFor;;
slow: slowTimedOut: [10.0r64 sleepFor canceled? ["canceled" toString] ["finished" toString] if] String 0.01r64 runFor;;
("fast=" fast " timedOut=" fastTimedOut LF) printList
("slow=" slow " timedOut=" slowTimedOut LF) printList
0
] "main" exportFunction
Expected Output
fast=42 timedOut=FALSE
slow=canceled timedOut=TRUE
See also
- sync/sync: Cross-platform scheduling, sleep, time, IPv4 formatting, and TCP helpers.
- sync/Context: Spawned context handle with waiting, output retrieval, and cancellation.