async_std/task/
spawn_blocking.rs

1use crate::task::{self, JoinHandle};
2
3/// Spawns a blocking task.
4///
5/// The task will be spawned onto a thread pool specifically dedicated to blocking tasks. This
6/// is useful to prevent long-running synchronous operations from blocking the main futures
7/// executor.
8///
9/// See also: [`task::block_on`], [`task::spawn`].
10///
11/// [`task::block_on`]: fn.block_on.html
12/// [`task::spawn`]: fn.spawn.html
13///
14/// # Examples
15///
16/// Basic usage:
17///
18/// ```
19/// # async_std::task::block_on(async {
20/// #
21/// use async_std::task;
22///
23/// task::spawn_blocking(|| {
24///     println!("long-running task here");
25/// })
26/// .await;
27/// #
28/// # })
29/// ```
30#[inline]
31pub fn spawn_blocking<F, T>(f: F) -> JoinHandle<T>
32where
33    F: FnOnce() -> T + Send + 'static,
34    T: Send + 'static,
35{
36    task::spawn(async_global_executor::spawn_blocking(f))
37}