pub fn spawn_blocking<F, R>(f: F) -> JoinHandle<R> ⓘ
Available on crate feature
blocking
only.Expand description
Run 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.
§Examples
use tokio::task;
let res = task::spawn_blocking(move || {
// do some compute-heavy work or call synchronous code
"done computing"
}).await?;
assert_eq!(res, "done computing");