Skip to content

Commit e75c3a9

Browse files
authored
Merge pull request #294 from async-rs/blocking-docs
add task::blocking docs
2 parents a06a52d + fc904a2 commit e75c3a9

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

Diff for: src/task/block_on.rs

+4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ use kv_log_macro::trace;
1919
/// Calling this function is similar to [spawning] a thread and immediately [joining] it, except an
2020
/// asynchronous task will be spawned.
2121
///
22+
/// See also: [`task::blocking`].
23+
///
24+
/// [`task::blocking`]: fn.blocking.html
25+
///
2226
/// [spawning]: https://doc.rust-lang.org/std/thread/fn.spawn.html
2327
/// [joining]: https://doc.rust-lang.org/std/thread/struct.JoinHandle.html#method.join
2428
///

Diff for: src/task/mod.rs

+23-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,29 @@ pub(crate) mod blocking;
5151

5252
/// Spawns a blocking task.
5353
///
54-
/// The task will be spawned onto a thread pool specifically dedicated to blocking tasks.
54+
/// The task will be spawned onto a thread pool specifically dedicated to blocking tasks. This
55+
/// is useful to prevent long-running synchronous operations from blocking the main futures
56+
/// executor.
57+
///
58+
/// See also: [`task::block_on`].
59+
///
60+
/// [`task::block_on`]: fn.block_on.html
61+
///
62+
/// # Examples
63+
///
64+
/// Basic usage:
65+
///
66+
/// ```
67+
/// # fn main() { async_std::task::block_on(async {
68+
/// #
69+
/// use async_std::task;
70+
///
71+
/// task::blocking(async {
72+
/// println!("long-running task here");
73+
/// }).await;
74+
/// #
75+
/// # }) }
76+
/// ```
5577
// Once this function stabilizes we should merge `blocking::spawn` into this so
5678
// all code in our crate uses `task::blocking` too.
5779
#[cfg(any(feature = "unstable", feature = "docs"))]

0 commit comments

Comments
 (0)