Documentation for tokio crate. Keywords: async, runtime, spawn, task, sync, mpsc, oneshot, broadcast, watch, mutex, rwlock, tcp, udp, socket, sleep, timeout, interval, timer, io, net, fs, process, signal
Version: 1.50.0 | Source: docs.rs
Tokio is an event-driven, non-blocking I/O platform for writing asynchronous applications with Rust. It provides a runtime, synchronization primitives, networking, timers, and more.
# Enable all features
tokio = { version = "1", features = ["full"] }
# Minimal features for spawning tasks and networking
tokio = { version = "1", features = ["rt", "net"] }
Spawn and manage concurrent tasks.
| Function | Description |
|---|---|
spawn |
Spawn a new async task, returns JoinHandle |
spawn_blocking | Run blocking code on a dedicated thread pool |
spawn_local | Spawn !Send future on LocalSet |
block_in_place | Transition current thread to blocking (multi-thread only) |
yield_now | Yield execution back to the runtime |
use tokio::task;
let handle = task::spawn(async {
"result"
});
let result = handle.await?;
Channels:
| Channel | Producers | Consumers | Values |
|---|---|---|---|
oneshot | 1 | 1 | 1 |
mpsc | Many | 1 | Many |
broadcast | Many | Many | Many |
watch | Many | Many | 1 (latest) |
Locks:
| Type | Description |
|---|---|
Mutex | Async mutual exclusion (hold across .await) |
RwLock | Multiple readers, single writer |
Semaphore | Limit concurrency with permits |
Barrier | Synchronize multiple tasks |
Notify | Wake a single task without data |
use tokio::sync::mpsc;
let (tx, mut rx) = mpsc::channel(100);