Compute calls
A function declared with compute def runs on the VM’s compute pool, one thread per scheduler, and the calling task waits while the other tasks go on. Use it for a call that may take long: a query, a compression, a model step.
extern "libsqlite3.so.0"
## Sleeps for ms milliseconds off the scheduler.
compute def sqlite3_sleep(ms: C.Int32) -> C.Int32
end
def main() -> Void
sleeper = Task.spawn(&sqlite3_sleep, 200)
puts("main goes on")
puts(Task.await(sleeper))
end- The result is the plain C value, as for
def. Acompute defis public like any other, unless the block comes afterprivate. - Nothing stops a compute call in the middle: it has no timeout, and
--max-stepsdoes not end it. For a limit, run the call in a task and useTask.await(task, ms). A cancel skips a call that has not started; a running call runs on, the cancelled task stops waiting at once, and the result is dropped. The program does not wait for a running call at the end. - A call that never returns holds a pool thread for the rest of the run. Once
--schedulerssuch calls are stuck, every later compute call waits. - The arguments are copied before the call and a
C.Stringresult as it returns. The function runs on another thread, so it must be safe to call from one, or run under aNative.Mutex.