Skip to content

How does Tokio decide how many threads to spawn/use and when? #3858

Answered by Darksonn
ilmoi asked this question in Q&A
Discussion options

You must be logged in to vote

Tokio has two kinds of threads:

  1. Worker threads. These run the tasks you spawn with tokio::spawn
  2. Blocking threads. These run the tasks you spawn with tokio::task::spawn_blocking.

To answer your question, we need to consider these separately.

Worker threads. The number of worker threads never changes. Each worker thread can execute an unbounded number of asynchronous tasks concurrently. When you start the runtime, it looks up how many CPU cores you have and spawns that many worker threads. You can configure the amount of worker threads on the runtime builder, or as an option to #[tokio::main]. Asynchronous tasks may be moved from one worker thread to another at any .await to more evenly d…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@ilmoi
Comment options

@emogames
Comment options

Answer selected by Darksonn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants