[][src]Struct tokio_compat::runtime::Runtime

pub struct Runtime { /* fields omitted */ }
This is supported on (feature="rt-current-thread" or feature="rt-full") and feature="rt-full" only.

A thread pool runtime that can run tasks that use both tokio 0.1 and tokio 0.2 APIs.

This functions similarly to the tokio::runtime::Runtime struct in the tokio crate. However, unlike that runtime, the tokio-compat runtime is capable of running both std::future::Future tasks that use tokio 0.2 runtime services. and futures 0.1 tasks that use tokio 0.1 runtime services.

Methods

impl Runtime[src]

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

This is supported on feature="rt-current-thread" or feature="rt-full" only.

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_compat::run.

See module level documentation for more details.

Examples

Creating a new Runtime with default configuration values.

use tokio_compat::runtime::Runtime;

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

// Use the runtime...

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

This is supported on feature="rt-current-thread" or feature="rt-full" only.

Return a handle to the runtime's executor.

The returned handle can be used to spawn both futures 0.1 and std::future tasks that run on this runtime.

Examples

use tokio_compat::runtime::Runtime;

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

let executor_handle = rt.executor();

// use `executor_handle`

pub fn spawn<F>(&self, future: F) -> &Self where
    F: Future01<Item = (), Error = ()> + Send + 'static, 
[src]

This is supported on feature="rt-current-thread" or feature="rt-full" only.

Spawn a futures 0.1 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_compat::runtime::Runtime;
use futures_01::future::Future;

fn main() {
   // Create the runtime
   let rt = Runtime::new().unwrap();

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

    rt.shutdown_on_idle()
        .wait()
        .unwrap();
}

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 spawn_std<F>(&self, future: F) -> &Self where
    F: Future<Output = ()> + Send + 'static, 
[src]

This is supported on feature="rt-current-thread" or feature="rt-full" only.

Spawn a std::future 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_compat::runtime::Runtime;
use futures_01::future::Future;

fn main() {
   // Create the runtime
   let rt = Runtime::new().unwrap();

   // Spawn a future onto the runtime
   rt.spawn_std(async {
       println!("now running on a worker thread");
   });

    rt.shutdown_on_idle()
        .wait()
        .unwrap();
}

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 spawn_handle<F>(
    &self,
    future: F
) -> JoinHandle<Result<F::Item, F::Error>> where
    F: Future01 + Send + 'static,
    F::Item: Send + 'static,
    F::Error: Send + 'static, 
[src]

This is supported on feature="rt-current-thread" or feature="rt-full" only.

Spawn a futures 0.1 future onto the Tokio runtime, returning a JoinHandle that can be used to await its result.

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.

Note that futures spawned in this manner do not "count" towards shutdown_on_idle, since they are paired with a JoinHandle for awaiting their completion. See here for details on shutting down the compatibility runtime.

See module level documentation for more details.

Examples

use tokio_compat::runtime::Runtime;
// Create the runtime
let rt = Runtime::new().unwrap();
let executor = rt.executor();

// Spawn a `futures` 0.1 future onto the runtime
executor.spawn(futures_01::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 spawn_handle_std<F>(&self, future: F) -> JoinHandle<F::Output> where
    F: Future + Send + 'static,
    F::Output: Send + 'static, 
[src]

This is supported on feature="rt-current-thread" or feature="rt-full" only.

Spawn a std::future future onto the Tokio runtime, returning a JoinHandle that can be used to await its result.

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.

Note that futures spawned in this manner do not "count" towards shutdown_on_idle, since they are paired with a JoinHandle for awaiting their completion. See here for details on shutting down the compatibility runtime.

See module level documentation for more details.

Examples

use tokio_compat::runtime::Runtime;

// Create the runtime
let rt = Runtime::new().unwrap();
let executor = rt.executor();

// Spawn a `std::future` future onto the runtime
executor.spawn_std(async {
    println!("now running on a worker thread");
});

pub fn block_on<F>(&mut self, future: F) -> Result<F::Item, F::Error> where
    F: Future01
[src]

This is supported on feature="rt-current-thread" or feature="rt-full" only.

Run a futures 0.1 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_std<F>(&mut self, future: F) -> F::Output where
    F: Future
[src]

This is supported on feature="rt-current-thread" or feature="rt-full" only.

Run a std::future 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 shutdown_on_idle(self) -> Shutdown[src]

This is supported on feature="rt-current-thread" or feature="rt-full" only.

Signals the runtime to shutdown once it becomes idle.

Blocks the current thread until 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.

Note: tasks spawned with associated JoinHandles do not "count" towards shutdown_on_idle. Since shutdown_on_idle does not exist in tokio 0.2, this is intended as a transitional API; its use should be phased out in favor of waiting on JoinHandles.

Examples

use tokio_compat::runtime::Runtime;
use futures_01::future::Future;

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

// Use the runtime...

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

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

This is supported on feature="rt-current-thread" or feature="rt-full" only.

Signals the runtime to shutdown immediately.

Blocks the current thread until 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_compat::runtime::Runtime;
use futures_01::future::Future;

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

// Use the runtime...

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

Trait Implementations

impl Debug for Runtime[src]

impl Drop for Runtime[src]

Auto Trait Implementations

impl !RefUnwindSafe for Runtime

impl Send for Runtime

impl Sync for Runtime

impl Unpin for Runtime

impl !UnwindSafe for Runtime

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.