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.
Agent 可直接安装
这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。
npx -y tokrepo@latest install 412fed82-3634-11f1-9bc6-00163e2b0d79 --target codex先 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.
常见问题
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.
引用来源 (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
讨论
相关资产
Makepad — Creative Software Development Platform for Rust
Makepad is a Rust-based UI framework and live-coding IDE that compiles to WebGL, Metal, DirectX, and OpenGL. It combines a visual design environment with a high-performance rendering engine for building desktop, mobile, and web applications.
Cloud Hypervisor — Lightweight Virtual Machine Monitor in Rust
Cloud Hypervisor is a KVM-based virtual machine monitor written in Rust for modern cloud workloads. It provides a minimal attack surface, fast boot times, and support for CPU/memory/device hotplug with a focus on security and simplicity.
WinUI 3 — Modern Native UI Framework for Windows Apps
Build polished Windows desktop applications using Microsoft's latest native UI framework with Fluent Design, XAML, and the Windows App SDK.
Pomerium — Identity-Aware Zero Trust Access Proxy
Pomerium is an open source reverse proxy that provides secure, identity-aware access to internal applications without a VPN, implementing BeyondCorp-style zero trust networking with SSO integration.