Tokio — Async Runtime for Reliable Rust Applications
Tokio is a runtime for writing reliable asynchronous applications with Rust. Provides async I/O, networking, scheduling, and timers. The foundation of most async Rust projects including Axum, Hyper, Tonic, and the broader Rust web ecosystem.
Instalación lista para agent
Este activo puede instalarse después de elegir el runtime, revisar el plan y ejecutar el comando correspondiente.
npx -y tokrepo@latest install 412fed82-3634-11f1-9bc6-00163e2b0d79 --target codexEjecutar después de confirmar el plan con dry-run.
What it is
Tokio is a runtime for writing reliable asynchronous applications with Rust. It provides async I/O, networking, scheduling, timers, and synchronization primitives. Frameworks like Axum, Hyper, and Tonic build on top of Tokio, making it the foundation of the Rust async ecosystem.
Tokio targets backend engineers who need high-throughput, low-latency network services. If you write HTTP servers, gRPC services, or database drivers in Rust, you almost certainly depend on Tokio.
How it saves time or tokens
Tokio replaces hand-rolled thread pools and event loops with a well-tested, work-stealing scheduler. Instead of managing OS threads manually, you spawn lightweight tasks that Tokio multiplexes across a small thread pool. A single Tokio runtime can handle tens of thousands of concurrent connections on commodity hardware. The ecosystem of compatible crates (reqwest, sqlx, tonic) means you rarely write low-level networking code.
How to use
- Add Tokio to your
Cargo.toml:
[dependencies]
tokio = { version = "1", features = ["full"] }
- Write an async main function:
#[tokio::main]
async fn main() {
println!("Hello from Tokio");
}
- Spawn concurrent tasks:
use tokio::time::{sleep, Duration};
#[tokio::main]
async fn main() {
let handle = tokio::spawn(async {
sleep(Duration::from_secs(1)).await;
42
});
let result = handle.await.unwrap();
println!("Got: {result}");
}
Example
A minimal TCP echo server using Tokio:
use tokio::net::TcpListener;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let listener = TcpListener::bind("127.0.0.1:8080").await?;
loop {
let (mut socket, _) = listener.accept().await?;
tokio::spawn(async move {
let mut buf = [0; 1024];
loop {
let n = socket.read(&mut buf).await.unwrap();
if n == 0 { return; }
socket.write_all(&buf[..n]).await.unwrap();
}
});
}
}
Related on TokRepo
- Coding tools — AI-assisted development resources for building applications
- Featured workflows — curated high-quality workflows across categories
Common pitfalls
- Calling blocking code (std::fs, heavy computation) inside async tasks starves the runtime. Use
tokio::task::spawn_blockingfor CPU-bound or blocking operations. - Forgetting to enable the
macrosfeature flag means#[tokio::main]and#[tokio::test]will not compile. - Holding a
MutexGuardacross an.awaitpoint causes deadlocks. Usetokio::sync::Mutexor restructure to drop the guard before awaiting.
Preguntas frecuentes
Both are async runtimes for Rust. Tokio uses a work-stealing scheduler and has a larger ecosystem of compatible crates (Axum, Hyper, Tonic). async-std follows the standard library API more closely. Most production Rust projects choose Tokio due to ecosystem breadth.
Yes. Axum is built on top of Tokio and requires the Tokio runtime. When you add Axum as a dependency, Tokio is pulled in transitively, but you still need `#[tokio::main]` on your entry point.
Tokio can handle tens of thousands of concurrent connections on a single machine. Each connection is a lightweight task, not an OS thread. The actual limit depends on available memory, file descriptor limits, and application logic.
Tokio is optimized for I/O-bound workloads. For CPU-bound tasks, use `tokio::task::spawn_blocking` to offload work to a dedicated thread pool, keeping the async scheduler responsive for I/O tasks.
Yes. Use `#[tokio::main(flavor = "current_thread")]` for a single-threaded runtime. This is useful for embedded environments, WASM targets, or when you want deterministic task ordering.
Referencias (3)
- Tokio GitHub— Tokio provides async I/O, networking, scheduling, and timers for Rust
- Tokio Documentation— Work-stealing scheduler and multi-threaded runtime design
- Tokio Ecosystem— Axum, Hyper, and Tonic build on Tokio
Relacionados en TokRepo
Discusión
Activos relacionados
Robyn — High-Performance Python Web Framework with Rust Runtime
A fast Python web framework that uses a Rust-based async runtime for request handling while keeping the developer experience in pure Python.
SeaORM — Async Dynamic ORM for Rust
SeaORM is an async and dynamic ORM for Rust built on top of SeaQuery. It supports MySQL, PostgreSQL, and SQLite with compile-time checked queries, automatic migrations, and an ergonomic API for building database-backed Rust applications.
Embassy — Async Embedded Framework for Rust Microcontrollers
A modern embedded framework for Rust that brings async/await to microcontrollers, enabling concurrent firmware without a traditional RTOS.
Axum — Ergonomic Modular Web Framework for Rust
Axum is a web application framework built on Tokio, Tower, and Hyper. Focuses on ergonomics and modularity with a macro-free routing API, seamless Tower middleware integration, and type-safe extractors. The official Tokio team web framework.