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
sourceimpl Builder
impl Builder
sourcepub fn new() -> Builder
pub fn new() -> Builder
Returns a new runtime builder initialized with default configuration values.
Configuration methods can be chained on the return value.
sourcepub fn enable_all(&mut self) -> &mut Self
pub fn enable_all(&mut self) -> &mut Self
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();
sourcepub fn num_threads(&mut self, val: usize) -> &mut Self
👎 Deprecated: In future will be replaced by core_threads method
pub fn num_threads(&mut self, val: usize) -> &mut Self
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.
sourcepub fn core_threads(&mut self, val: usize) -> &mut Self
pub fn core_threads(&mut self, val: usize) -> &mut Self
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();
sourcepub fn max_threads(&mut self, val: usize) -> &mut Self
pub fn max_threads(&mut self, val: usize) -> &mut Self
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
.
sourcepub fn thread_name(&mut self, val: impl Into<String>) -> &mut Self
pub fn thread_name(&mut self, val: impl Into<String>) -> &mut Self
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();
sourcepub fn thread_stack_size(&mut self, val: usize) -> &mut Self
pub fn thread_stack_size(&mut self, val: usize) -> &mut Self
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();
sourcepub fn on_thread_start<F>(&mut self, f: F) -> &mut Self where
F: Fn() + Send + Sync + 'static,
pub fn on_thread_start<F>(&mut self, f: F) -> &mut Self where
F: Fn() + Send + Sync + 'static,
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();
sourcepub fn on_thread_stop<F>(&mut self, f: F) -> &mut Self where
F: Fn() + Send + Sync + 'static,
pub fn on_thread_stop<F>(&mut self, f: F) -> &mut Self where
F: Fn() + Send + Sync + 'static,
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();
sourceimpl Builder
impl Builder
sourcepub fn enable_time(&mut self) -> &mut Self
pub fn enable_time(&mut self) -> &mut Self
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();
sourceimpl Builder
impl Builder
sourcepub fn basic_scheduler(&mut self) -> &mut Self
pub fn basic_scheduler(&mut self) -> &mut Self
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.
sourceimpl Builder
impl Builder
sourcepub fn threaded_scheduler(&mut self) -> &mut Self
pub fn threaded_scheduler(&mut self) -> &mut Self
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
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
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more