pub fn block_in_place<F, R>(f: F) -> Rwhere
F: FnOnce() -> R,
Available on crate features
blocking
and rt-threaded
only.Expand description
Run the provided blocking function without blocking the executor.
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. If you run a closure through this method, the current executor thread will relegate all its executor duties to another (possibly new) thread, and only then poll the task. Note that this requires additional synchronization.
§Note
This function can only be called from a spawned task when working with the threaded scheduler. Consider using tokio::task::spawn_blocking.
§Examples
use tokio::task;
task::block_in_place(move || {
// do some compute-heavy work or call synchronous code
});