pub struct TokioRuntime {
pub tokio: Runtime,
pub config: TokioConfig,
pub counters: Arc<ThreadCounters>,
}agave-unstable-api crate feature must be specified to acknowledge use of an interface that may break without warning.Fieldsยง
ยงtokio: Runtimeagave-unstable-api crate feature must be specified to acknowledge use of an interface that may break without warning.config: TokioConfigagave-unstable-api crate feature must be specified to acknowledge use of an interface that may break without warning.counters: Arc<ThreadCounters>agave-unstable-api crate feature must be specified to acknowledge use of an interface that may break without warning.Implementationsยง
Sourceยงimpl TokioRuntime
impl TokioRuntime
Sourcepub fn start_metrics_sampling(&self, period: Duration)
๐Deprecated since 3.1.0: This crate has been marked for formal inclusion in the Agave Unstable API. From v4.0.0 onward, the agave-unstable-api crate feature must be specified to acknowledge use of an interface that may break without warning.
pub fn start_metrics_sampling(&self, period: Duration)
agave-unstable-api crate feature must be specified to acknowledge use of an interface that may break without warning.Starts the metrics sampling task on the runtime to monitor how many workers are busy doing useful things.
pub fn new(name: String, cfg: TokioConfig) -> Result<Self>
agave-unstable-api crate feature must be specified to acknowledge use of an interface that may break without warning.Methods from Deref<Target = Runtime>ยง
Sourcepub fn handle(&self) -> &Handle
pub fn handle(&self) -> &Handle
Returns a handle to the runtimeโs spawner.
The returned handle can be used to spawn tasks that run on this runtime, and can
be cloned to allow moving the Handle to other threads.
Calling Handle::block_on on a handle to a current_thread runtime is error-prone.
Refer to the documentation of Handle::block_on for more.
ยงExamples
use tokio::runtime::Runtime;
let rt = Runtime::new()
.unwrap();
let handle = rt.handle();
// Use the handle...Sourcepub fn spawn<F>(&self, future: F) -> JoinHandle<<F as Future>::Output>
pub fn spawn<F>(&self, future: F) -> JoinHandle<<F as Future>::Output>
Spawns a future onto the Tokio runtime.
This spawns the given future onto the runtimeโs executor, usually a thread pool. The thread pool is then responsible for polling the future until it completes.
The provided future will start running in the background immediately
when spawn is called, even if you donโt await the returned
JoinHandle.
See module level documentation for more details.
ยงExamples
use tokio::runtime::Runtime;
// Create the runtime
let rt = Runtime::new().unwrap();
// Spawn a future onto the runtime
rt.spawn(async {
println!("now running on a worker thread");
});Sourcepub fn spawn_blocking<F, R>(&self, func: F) -> JoinHandle<R>
pub fn spawn_blocking<F, R>(&self, func: F) -> JoinHandle<R>
Runs the provided function on an executor dedicated to blocking operations.
ยงExamples
use tokio::runtime::Runtime;
// Create the runtime
let rt = Runtime::new().unwrap();
// Spawn a blocking function onto the runtime
rt.spawn_blocking(|| {
println!("now running on a worker thread");
});Sourcepub fn block_on<F>(&self, future: F) -> <F as Future>::Outputwhere
F: Future,
pub fn block_on<F>(&self, future: F) -> <F as Future>::Outputwhere
F: Future,
Runs a future to completion on the Tokio runtime. This is the runtimeโs entry point.
This runs the given future on the current thread, blocking until it is complete, and yielding its resolved result. Any tasks or timers which the future spawns internally will be executed on the runtime.
ยงNon-worker future
Note that the future required by this function does not run as a worker. The expectation is that other tasks are spawned by the future here. Awaiting on other futures from the future provided here will not perform as fast as those spawned as workers.
ยงMulti thread scheduler
When the multi thread scheduler is used this will allow futures to run within the io driver and timer context of the overall runtime.
Any spawned tasks will continue running after block_on returns.
ยงCurrent thread scheduler
When the current thread scheduler is enabled block_on
can be called concurrently from multiple threads. The first call
will take ownership of the io and timer drivers. This means
other threads which do not own the drivers will hook into that one.
When the first block_on completes, other threads will be able to
โstealโ the driver to allow continued execution of their futures.
Any spawned tasks will be suspended after block_on returns. Calling
block_on again will resume previously spawned tasks.
ยงPanics
This function panics if the provided future panics, or if called within an asynchronous execution context.
ยงExamples
use tokio::runtime::Runtime;
// Create the runtime
let rt = Runtime::new().unwrap();
// Execute the future, blocking the current thread until completion
rt.block_on(async {
println!("hello");
});Sourcepub fn enter(&self) -> EnterGuard<'_>
pub fn enter(&self) -> EnterGuard<'_>
Enters the runtime context.
This allows you to construct types that must have an executor
available on creation such as Sleep or TcpStream. It will
also allow you to call methods such as tokio::spawn.
ยงExample
use tokio::runtime::Runtime;
use tokio::task::JoinHandle;
fn function_that_spawns(msg: String) -> JoinHandle<()> {
// Had we not used `rt.enter` below, this would panic.
tokio::spawn(async move {
println!("{}", msg);
})
}
fn main() {
let rt = Runtime::new().unwrap();
let s = "Hello World!".to_string();
// By entering the context, we tie `tokio::spawn` to this executor.
let _guard = rt.enter();
let handle = function_that_spawns(s);
// Wait for the task before we end the test.
rt.block_on(handle).unwrap();
}Sourcepub fn metrics(&self) -> RuntimeMetrics
pub fn metrics(&self) -> RuntimeMetrics
Returns a view that lets you get information about how the runtime is performing.
Trait Implementationsยง
Sourceยงimpl Debug for TokioRuntime
impl Debug for TokioRuntime
Auto Trait Implementationsยง
impl !Freeze for TokioRuntime
impl RefUnwindSafe for TokioRuntime
impl Send for TokioRuntime
impl Sync for TokioRuntime
impl Unpin for TokioRuntime
impl UnwindSafe for TokioRuntime
Blanket Implementationsยง
Sourceยงimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Sourceยงfn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Sourceยงimpl<T> Instrument for T
impl<T> Instrument for T
Sourceยงfn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Sourceยงfn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Sourceยงimpl<T> IntoEither for T
impl<T> IntoEither for T
Sourceยงfn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSourceยงfn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more