Module tokio
Asynchronous runtime for writing reliable network applications.
tokio is an asynchronous runtime for the Rust programming language.
It provides the building blocks needed for writing network applications
without compromising speed.
At a high level, Tokio provides:
- A multi-threaded, work-stealing based task scheduler
- A reactor backed by the operating system's event queue (epoll, kqueue, IOCP, etc.)
- Asynchronous TCP and UDP sockets
- Asynchronous filesystem operations
- Timer and timeout utilities
Tokio is built using the async/await syntax introduced in Rust 1.39. The runtime schedules asynchronous tasks cooperatively, allowing thousands of tasks to run concurrently on a small number of threads.
The Runtime provides the execution environment for async code.
Most applications will use the tokio::main macro to set up the runtime,
though it can also be constructed manually for more control.
Tokio's networking primitives like TcpListener and TcpStream
are designed to work seamlessly with async/await,
making it straightforward to write servers and clients.
Examples
Creating a simple TCP echo server:
use ;
use ;
async
async
Using timers and timeouts:
use ;
async
async
Spawning concurrent tasks:
use task;
async
Modules
- fs Asynchronous file utilities.
- io Traits, helpers, and type definitions for asynchronous I/O functionality.
-
net
TCP/UDP/Unix bindings for
tokio. - process An implementation of asynchronous process management for Tokio.
- runtime The Tokio runtime.
- signal Asynchronous signal handling for Tokio.
-
stream
Due to the
Streamtrait's inclusion instdlanding later than Tokio's 1.0 release, most of the Tokio stream utilities have been moved into thetokio-streamcrate. - sync Synchronization primitives for use in asynchronous contexts.
- task Asynchronous green-threads.
- time Utilities for tracking time.
Macros
- join Waits on multiple concurrent branches, returning when all branches complete.
- pin Pins a value on the stack.
- select Waits on multiple concurrent branches, returning when the first branch completes, cancelling the remaining branches.
-
task_local
Declares a new task-local key of type
tokio::task::LocalKey. -
try_join
Waits on multiple concurrent branches, returning when all branches
complete with
Ok(_)or on the firstErr(_).