[][src]Struct tokio::runtime::Runtime

pub struct Runtime { /* fields omitted */ }

Handle to the Tokio runtime.

The Tokio runtime includes a reactor as well as an executor for running tasks.

Instances of Runtime can be created using new or Builder. However, most users will use tokio::run, which uses a Runtime internally.

See module level documentation for more details.

Methods

impl Runtime[src]

pub fn new() -> Result<Self>[src]

Create a new runtime instance with default configuration values.

This results in a reactor, thread pool, and timer being initialized. The thread pool will not spawn any worker threads until it needs to, i.e. tasks are scheduled to run.

Most users will not need to call this function directly, instead they will use tokio::run.

See module level documentation for more details.

Examples

Creating a new Runtime with default configuration values.

use tokio::runtime::Runtime;
use tokio::prelude::*;

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

// Use the runtime...

// Shutdown the runtime
rt.shutdown_now()
    .wait().unwrap();

pub fn reactor(&self) -> &Handle[src]

Deprecated since 0.1.11:

there is now a reactor per worker thread

Return a reference to the reactor handle for this runtime instance.

The returned handle reference can be cloned in order to get an owned value of the handle. This handle can be used to initialize I/O resources (like TCP or UDP sockets) that will not be used on the runtime.

Examples

use tokio::runtime::Runtime;

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

let reactor_handle = rt.reactor().clone();

// use `reactor_handle`

pub fn executor(&self) -> TaskExecutor[src]

Return a handle to the runtime's executor.

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 executor_handle = rt.executor();

// use `executor_handle`

Important traits for &'_ mut R
pub fn spawn<F>(&mut self, future: F) -> &mut Self where
    F: Future<Item = (), Error = ()> + Send + 'static, 
[src]

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 mut rt = Runtime::new().unwrap();

// Spawn a future onto the runtime
rt.spawn(future::lazy(|| {
    println!("now running on a worker thread");
    Ok(())
}));

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.

pub fn block_on<F, R, E>(&mut self, future: F) -> Result<R, E> where
    F: Send + 'static + Future<Item = R, Error = E>,
    R: Send + 'static,
    E: Send + 'static, 
[src]

Run a future to completion on the Tokio runtime.

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.

pub fn block_on_all<F, R, E>(self, future: F) -> Result<R, E> where
    F: Send + 'static + Future<Item = R, Error = E>,
    R: Send + 'static,
    E: Send + 'static, 
[src]

Run a future to completion on the Tokio runtime, then wait for all background futures to complete too.

This runs the given future on the runtime, blocking until it is complete, waiting for background futures to complete, and yielding its resolved result. Any tasks or timers which the future spawns internally will be executed on the runtime and waited for completion.

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.

pub fn shutdown_on_idle(self) -> Shutdown[src]

Signals the runtime to shutdown once it becomes idle.

Returns a future that completes once the shutdown operation has completed.

This function can be used to perform a graceful shutdown of the runtime.

The runtime enters an idle state once all of the following occur.

  • The thread pool has no tasks to execute, i.e., all tasks that were spawned have completed.
  • The reactor is not managing any I/O resources.

See module level documentation for more details.

Examples

use tokio::runtime::Runtime;
use tokio::prelude::*;

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

// Use the runtime...

// Shutdown the runtime
rt.shutdown_on_idle()
    .wait().unwrap();

pub fn shutdown_now(self) -> Shutdown[src]

Signals the runtime to shutdown immediately.

Returns a future that completes once the shutdown operation has completed.

This function will forcibly shutdown the runtime, causing any in-progress work to become canceled. The shutdown steps are:

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

See module level documentation for more details.

Examples

use tokio::runtime::Runtime;
use tokio::prelude::*;

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

// Use the runtime...

// Shutdown the runtime
rt.shutdown_now()
    .wait().unwrap();

Trait Implementations

impl Drop for Runtime[src]

impl Debug for Runtime[src]

Auto Trait Implementations

impl Send for Runtime

impl !Unpin for Runtime

impl Sync for Runtime

impl !UnwindSafe for Runtime

impl !RefUnwindSafe for Runtime

Blanket Implementations

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.

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

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

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

impl<T> Erased for T