Overview
Unlike other Rust programs, asynchronous applications require runtime support. The TokioRuntime provides:
- I/O event loop: Drives I/O resources and dispatches I/O events to tasks
- Scheduler: Executes tasks that use I/O resources
- Timer: Schedules work to run after a set period of time
Creating a Runtime
Runtime Types
Multi-Thread Runtime
The multi-thread scheduler executes futures on a thread pool using a work-stealing strategy. By default, it starts a worker thread for each CPU core. This is the ideal configuration for most applications.Current-Thread Runtime
The current-thread scheduler provides a single-threaded future executor. All tasks are created and executed on the current thread.Builder
TheBuilder type allows for custom runtime configuration.
Functions
Creates a new multi-thread runtime builder. Returns a
Builder configured for the multi-threaded scheduler.Creates a new current-thread runtime builder. Returns a
Builder configured for the current-thread scheduler.Builder Methods
Sets the number of worker threads for the runtime.Parameters:
val: Number of worker threads (only for multi-thread runtime)
Sets the name for threads spawned by the runtime.Parameters:
name: Thread name prefix
Sets the stack size for threads spawned by the runtime.Parameters:
size: Stack size in bytes
Enables both I/O and time drivers.Example:
Enables the I/O driver. Required for using networking and I/O types.Example:
Enables the time driver. Required for using time-related functionality.Example:
Builds the configured runtime. Returns
Result<Runtime, io::Error>.Example:Runtime Struct
Methods
Creates a new multi-thread runtime with default configuration.Returns:
Result<Runtime, io::Error>Example:Runs a future to completion on the runtime, blocking the current thread until done.Parameters:
future: The future to execute
Spawns a future onto the runtime.Parameters:
future: The future to spawn
JoinHandle for the spawned taskExample:Returns a handle to the runtime. The handle can be cloned and used to spawn tasks from non-async contexts.Example:
Handle
AHandle to a runtime allows spawning tasks without owning the runtime.
Methods
Returns a handle to the current runtime. Panics if called outside a runtime context.Example:
Spawns a future onto the runtime associated with this handle.Example:
Enters the runtime context. Returns a guard that maintains the runtime context until dropped.Example:
Runtime Behavior
Fairness Guarantee
Tokio provides fairness guarantees for task scheduling:If the total number of tasks does not grow without bound, and no task is blocking the thread, then tasks are guaranteed to be scheduled fairly.
Cooperative Scheduling
Tasks use cooperative multitasking. Each task should periodically yield control back to the scheduler by awaiting. Tasks that run for too long without yielding may starve other tasks.Shutdown
When aRuntime is dropped, all spawned tasks are cancelled. The runtime waits for all worker threads to terminate.
Configuration Options
Global Queue Interval
Controls how many ticks before pulling a task from the global queue. Can be tuned for performance.
Event Interval
Controls how many ticks before yielding to the driver for timer and I/O events. Default is 61 ticks.
LIFO Slot
The multi-threaded runtime uses a LIFO slot optimization for task scheduling. Can be disabled if needed.
Best Practices
Choose the right runtime
Use multi-thread runtime for most applications. Use current-thread runtime for single-threaded scenarios or when
!Send futures are needed.Avoid blocking operations
Use
spawn_blocking for CPU-intensive or blocking operations to avoid blocking the runtime threads.