Sub topic 5
Choosing a runtime flavor and worker threads
This chapter assumes you can start a runtime with #[tokio::main] and spawn tasks with tokio::spawn. It covers the two runtime flavors Tokio offers and how to configure them.
The two flavors
Tokio ships two schedulers:
- Multi-thread runs tasks on a pool of worker threads. Idle workers steal tasks from busy ones. This is the default for
#[tokio::main]. - Current-thread runs every task on the thread that calls
block_on. There is no work stealing and no parallelism.
| Property | Multi-thread | Current-thread |
|---|---|---|
| Parallel execution | Yes | No |
| Tasks must be `Send` | Yes | Not for `spawn_local` tasks |
| Worker threads | One per CPU core by default | None added |
| Typical use | Servers, general programs | Tests, small tools, single-core targets |
Selecting a flavor
Pass flavor and worker_threads to the macro:
use std::time::Duration;
use tokio::time::sleep;
#[tokio::main(flavor = "multi_thread", worker_threads = 4)]
async fn main() {
let handle = tokio::spawn(async {
sleep(Duration::from_millis(50)).await;
"done"
});
println!("{}", handle.await.unwrap());
}For the current-thread flavor, use #[tokio::main(flavor = "current_thread")]. The worker_threads option applies only to the multi-thread flavor. Without it, Tokio starts one worker per CPU core. The TOKIO_WORKER_THREADS environment variable can override the count.
The macro is shorthand for building a runtime yourself. Building it directly gives the same result and is useful when you need control before the runtime starts:
use tokio::runtime::Builder;
fn main() {
let rt = Builder::new_multi_thread()
.worker_threads(2)
.enable_all()
.build()
.unwrap();
let value = rt.block_on(async { 1 + 1 });
println!("{value}");
}enable_all() turns on the I/O and time drivers. Without it, calls such as sleep or TcpListener::bind panic at runtime.
Choosing between them
Pick multi-thread when many tasks do real work at once, such as a server handling many connections. Pick current-thread when the program is mostly waiting on I/O at low volume, when you want deterministic scheduling in tests, or when the target has one core.
Common errors
- Blocking a worker. On a current-thread runtime, one blocking call stalls every task. On a multi-thread runtime it stalls one worker, and the rest keep going until enough workers are blocked. Move blocking work to
spawn_blocking, covered in a later chapter. - Assuming ordering. With multiple workers, spawned tasks may run in any order and at the same time. Do not rely on a task starting before another.
- Setting too many workers. More workers than cores adds context switching and does not add throughput for async I/O work.
- Forgetting
enable_all()when using the builder by hand.
Exercise
You write a small command-line tool that makes one HTTP request at a time and prints the result. Which flavor fits, and why? What changes if you later turn it into a server that handles thousands of simultaneous clients?
Show answer
The current-thread flavor fits. The program has no concurrent CPU work, so extra worker threads add overhead and no benefit. For a server with thousands of clients, switch to the multi-thread flavor (the default) so tasks can run in parallel across cores.