Skip to main content

spawn_blocking

Function spawn_blocking 

Source
pub fn spawn_blocking<F, R>(f: F) -> TaskHandle<R>
where F: FnOnce() -> R + Send + 'static, R: Send + 'static,
Expand description

Run a blocking closure on a worker thread.

Not the reactor capability. What this needs is a pool of threads that may block, not the I/O and time drivers, and the two are different requirements even though tokio happens to hang both off one runtime: on a hosted build this is tokio::task::spawn_blocking, which does require a runtime entered on the calling thread, and on exec_backend it is the callback pool, which requires nothing. That residual dual meaning is why Reactor::spawn_blocking exists beside it — a site that already holds a Reactor should use that one and say so in its signature. This free function stays for the callers whose work is reactor-free by nature (runtime::fs, waitpid, a name lookup) and for whom a process-global blocking pool, which this crate does not yet have, would be the right destination.