pub struct Builder { /* private fields */ }
Expand description

Builds Tokio Runtime with custom configuration values.

Methods can be chained in order to set the configuration values. The Runtime is constructed by calling build.

New instances of Builder are obtained via Builder::new.

See function level documentation for details on the various configuration settings.

Examples

use tokio::runtime::Builder;

fn main() {
    // build runtime
    let runtime = Builder::new()
        .threaded_scheduler()
        .core_threads(4)
        .thread_name("my-custom-name")
        .thread_stack_size(3 * 1024 * 1024)
        .build()
        .unwrap();

    // use runtime ...
}

Implementations

Returns a new runtime builder initialized with default configuration values.

Configuration methods can be chained on the return value.

Enables both I/O and time drivers.

Doing this is a shorthand for calling enable_io and enable_time individually. If additional components are added to Tokio in the future, enable_all will include these future components.

Examples
use tokio::runtime;

let rt = runtime::Builder::new()
    .threaded_scheduler()
    .enable_all()
    .build()
    .unwrap();
👎 Deprecated:

In future will be replaced by core_threads method

Sets the maximum number of worker threads for the Runtime’s thread pool.

This must be a number between 1 and 32,768 though it is advised to keep this value on the smaller side.

The default value is the number of cores available to the system.

Sets the core number of worker threads for the Runtime’s thread pool.

This should be a number between 1 and 32,768 though it is advised to keep this value on the smaller side.

The default value is the number of cores available to the system.

These threads will be always active and running.

Examples
use tokio::runtime;

let rt = runtime::Builder::new()
    .threaded_scheduler()
    .core_threads(4)
    .build()
    .unwrap();

Specifies limit for threads, spawned by the Runtime.

This is number of threads to be used by Runtime, including core_threads Having max_threads less than core_threads results in invalid configuration when building multi-threaded Runtime, which would cause a panic.

Similarly to the core_threads, this number should be between 1 and 32,768.

The default value is 512.

When multi-threaded runtime is not used, will act as limit on additional threads.

Otherwise as core_threads are always active, it limits additional threads (e.g. for blocking annotations) as max_threads - core_threads.

Sets name of threads spawned by the Runtime’s thread pool.

The default name is “tokio-runtime-worker”.

Examples

let rt = runtime::Builder::new()
    .thread_name("my-pool")
    .build();

Sets the stack size (in bytes) for worker threads.

The actual stack size may be greater than this value if the platform specifies minimal stack size.

The default stack size for spawned threads is 2 MiB, though this particular stack size is subject to change in the future.

Examples

let rt = runtime::Builder::new()
    .threaded_scheduler()
    .thread_stack_size(32 * 1024)
    .build();

Executes function f after each thread is started but before it starts doing work.

This is intended for bookkeeping and monitoring use cases.

Examples

let runtime = runtime::Builder::new()
    .threaded_scheduler()
    .on_thread_start(|| {
        println!("thread started");
    })
    .build();

Executes function f before each thread stops.

This is intended for bookkeeping and monitoring use cases.

Examples

let runtime = runtime::Builder::new()
    .threaded_scheduler()
    .on_thread_stop(|| {
        println!("thread stopping");
    })
    .build();

Creates the configured Runtime.

The returned ThreadPool instance is ready to spawn tasks.

Examples
use tokio::runtime::Builder;

let mut rt = Builder::new().build().unwrap();

rt.block_on(async {
    println!("Hello from the Tokio runtime");
});

Enables the I/O driver.

Doing this enables using net, process, signal, and some I/O types on the runtime.

Examples
use tokio::runtime;

let rt = runtime::Builder::new()
    .enable_io()
    .build()
    .unwrap();

Enables the time driver.

Doing this enables using tokio::time on the runtime.

Examples
use tokio::runtime;

let rt = runtime::Builder::new()
    .enable_time()
    .build()
    .unwrap();

Sets runtime to use a simpler scheduler that runs all tasks on the current-thread.

The executor and all necessary drivers will all be run on the current thread during block_on calls.

See also the module level documentation, which has a section on scheduler types.

Sets runtime to use a multi-threaded scheduler for executing tasks.

See also the module level documentation, which has a section on scheduler types.

Trait Implementations

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.