[][src]Function tokio::task::spawn_blocking

Important traits for JoinHandle<T>
pub fn spawn_blocking<F, R>(f: F) -> JoinHandle<R> where
    F: FnOnce() -> R + Send + 'static,
    R: Send + 'static, 
This is supported on feature="blocking" only.

Runs the provided closure on a thread where blocking is acceptable.

In general, issuing a blocking call or performing a lot of compute in a future without yielding is not okay, as it may prevent the executor from driving other futures forward. A closure that is run through this method will instead be run on a dedicated thread pool for such blocking tasks without holding up the main futures executor.


use tokio::task;

let res = task::spawn_blocking(move || {
    // do some compute-heavy work or call synchronous code
    "done computing"

assert_eq!(res, "done computing");