broker_tokio::runtime

Struct Runtime

Source
pub struct Runtime { /* private fields */ }
Expand description

The Tokio runtime.

The runtime provides an I/O driver, task scheduler, timer, and blocking pool, necessary for running asynchronous tasks.

Instances of Runtime can be created using new or Builder. However, most users will use the #[tokio::main] annotation on their entry point instead.

See module level documentation for more details.

§Shutdown

Shutting down the runtime is done by dropping the value. The current thread will block until the shut down operation has completed.

  • Drain any scheduled work queues.
  • Drop any futures that have not yet completed.
  • Drop the reactor.

Once the reactor has dropped, any outstanding I/O resources bound to that reactor will no longer function. Calling any method on them will result in an error.

Implementations§

Source§

impl Runtime

Source

pub fn new() -> Result<Self>

Create a new runtime instance with default configuration values.

This results in a scheduler, I/O driver, and time driver being initialized. The type of scheduler used depends on what feature flags are enabled: if the rt-threaded feature is enabled, the threaded scheduler is used, while if only the rt-core feature is enabled, the basic scheduler is used instead.

If the threaded scheduler is selected, it will not spawn any worker threads until it needs to, i.e. tasks are scheduled to run.

Most applications will not need to call this function directly. Instead, they will use the #[tokio::main] attribute. When more complex configuration is necessary, the runtime builder may be used.

See module level documentation for more details.

§Examples

Creating a new Runtime with default configuration values.

use tokio::runtime::Runtime;

let rt = Runtime::new()
    .unwrap();

// Use the runtime...
Source

pub fn spawn<F>(&self, future: F) -> JoinHandle<F::Output>
where F: Future + Send + 'static, F::Output: Send + 'static,

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

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

This function panics if the spawn fails. Failure occurs if the executor is currently at capacity and is unable to spawn a new future.

Source

pub fn block_on<F: Future>(&mut self, future: F) -> F::Output

Run a future to completion on the Tokio runtime. This is the runtime’s entry point.

This runs the given future on the runtime, 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.

This method should not be called from an asynchronous context.

§Panics

This function panics if the executor is at capacity, if the provided future panics, or if called within an asynchronous execution context.

Source

pub fn enter<F, R>(&self, f: F) -> R
where F: FnOnce() -> R,

Enter the runtime context

Source

pub fn handle(&self) -> &Handle

Return a handle to the runtime’s spawner.

The returned handle can be used to spawn tasks that run on this runtime.

§Examples
use tokio::runtime::Runtime;

let rt = Runtime::new()
    .unwrap();

let handle = rt.handle();

handle.spawn(async { println!("hello"); });

Trait Implementations§

Source§

impl Debug for Runtime

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.