Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions tokio/src/task/blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,21 @@ cfg_rt! {
/// their own. If you want to spawn an ordinary thread, you should use
/// [`thread::spawn`] instead.
///
/// # When to use `spawn_blocking` vs dedicated threads
///
/// `spawn_blocking` is intended for *bounded* blocking work that eventually finishes.
/// Each call occupies a thread from the runtime's blocking thread pool for the duration
/// of the task. Long-lived tasks therefore reduce the pool's effective capacity, which
/// can delay other blocking operations once the pool is saturated and work is queued.
///
/// For workloads that run indefinitely or for extended periods (for example,
/// background workers or persistent processing loops), prefer a dedicated thread created with
/// [`thread::spawn`].
///
/// As a rule of thumb:
/// - Use `spawn_blocking` for short-lived blocking operations
/// - Use dedicated threads for long-lived or persistent blocking workloads
///
/// Be aware that tasks spawned using `spawn_blocking` cannot be aborted
/// because they are not async. If you call [`abort`] on a `spawn_blocking`
/// task, then this *will not have any effect*, and the task will continue
Expand Down