Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc: mention CPU-bound code lib.rs #2414

Merged
merged 1 commit into from
Apr 19, 2020
Merged

Conversation

Darksonn
Copy link
Contributor

@Darksonn Darksonn commented Apr 18, 2020

This PR makes it easier to find information about blocking code in Tokio's documentation. It also makes something show up when ctrl-F ing for "CPU".

Rendered:

CPU-bound tasks and blocking code

Tokio is able to concurrently run many tasks on a few threads by repeatedly swapping the currently running task on each thread. However, this kind of swapping can only happen at .await points, so code that spends a long time without reaching an .await will prevent other tasks from running. To combat this, Tokio provides two kinds of threads: Core threads and blocking threads. The core threads are where all asynchronous code runs, and Tokio will by default spawn one for each CPU core. The blocking threads are spawned on demand, and can be used to run blocking code that would otherwise block other tasks from running. Since it is not possible for Tokio to swap out blocking tasks, like it can do with asynchronous code, the upper limit on the number of blocking threads is very large. These limits can be configured on the Builder.

Two spawn a blocking task, you should use the spawn_blocking function.

#[tokio::main]
async fn main() {
    // This is running on a core thread.

    let blocking_task = tokio::task::spawn_blocking(|| {
        // This is running on a blocking thread.
        // Blocking here is ok.
    });

    // We can wait for the blocking task like this:
    // If the blocking task panics, the unwrap below will propagate the
    // panic.
    blocking_task.await.unwrap();
}

If your code is CPU-bound and you wish to limit the number of threads used to run it, you should run it on another thread pool such as rayon. You can use an oneshot channel to send the result back to Tokio when the rayon task finishes.

@Darksonn Darksonn added C-enhancement Category: A PR with an enhancement or bugfix. T-docs Topic: documentation A-tokio Area: The main tokio crate S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. M-blocking Module: tokio/task/blocking labels Apr 18, 2020
@Darksonn Darksonn self-assigned this Apr 18, 2020
@Darksonn
Copy link
Contributor Author

Windows CI failed in a weird way with some sort of deadlock. I don't think it's related to this issue, so I am restarting CI. I have stored the log here so we can investigate later.

Copy link
Member

@carllerche carllerche left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! This is great.

@carllerche carllerche merged commit 800574b into tokio-rs:master Apr 19, 2020
@Darksonn Darksonn deleted the doc-blocking branch April 19, 2020 18:55
@Darksonn Darksonn removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio Area: The main tokio crate C-enhancement Category: A PR with an enhancement or bugfix. M-blocking Module: tokio/task/blocking T-docs Topic: documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants