[][src]Struct tokio::runtime::Builder

pub struct Builder { /* fields omitted */ }

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

impl Builder[src]

pub fn new() -> Builder[src]

Returns a new runtime builder initialized with default configuration values.

Configuration methods can be chained on the return value.

pub fn enable_all(&mut self) -> &mut Self[src]

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();

pub fn num_threads(&mut self, val: usize) -> &mut Self[src]

👎 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.

pub fn core_threads(&mut self, val: usize) -> &mut Self[src]

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();

pub fn max_threads(&mut self, val: usize) -> &mut Self[src]

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.

pub fn thread_name(&mut self, val: impl Into<String>) -> &mut Self[src]

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();

pub fn thread_stack_size(&mut self, val: usize) -> &mut Self[src]

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();

pub fn on_thread_start<F>(&mut self, f: F) -> &mut Self where
    F: Fn() + Send + Sync + 'static, 
[src]

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();

pub fn on_thread_stop<F>(&mut self, f: F) -> &mut Self where
    F: Fn() + Send + Sync + 'static, 
[src]

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();

pub fn build(&mut self) -> Result<Runtime>[src]

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");
});

impl Builder[src]

pub fn enable_io(&mut self) -> &mut Self[src]

This is supported on feature="io-driver" only.

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();

impl Builder[src]

pub fn enable_time(&mut self) -> &mut Self[src]

This is supported on feature="time" only.

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();

impl Builder[src]

pub fn basic_scheduler(&mut self) -> &mut Self[src]

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.

impl Builder[src]

pub fn threaded_scheduler(&mut self) -> &mut Self[src]

This is supported on feature="rt-threaded" only.

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

impl Debug for Builder[src]

impl Default for Builder[src]

Auto Trait Implementations

impl !RefUnwindSafe for Builder

impl Send for Builder

impl Sync for Builder

impl Unpin for Builder

impl !UnwindSafe for Builder

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.