[][src]Module tokio_compat::runtime::current_thread

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

A compatibility implementation that runs everything on the current thread.

current_thread::Runtime is similar to the primary compatibility Runtime except that it runs all components on the current thread instead of using a thread pool. This means that it is able to spawn futures that do not implement Send.

Same as the default current_thread::Runtime in the main tokio crate, the tokio_compat::current_thread::Runtime includes:

  • A reactor to drive I/O resources.
  • An executor to execute tasks that use these I/O resources.
  • A timer for scheduling work to run after a set period of time.

Unlike the default current_thread::Runtime, however, the tokio_compat version must spawn an additional background thread to run the tokio 0.1 Reactor and Timer. This is necessary to support legacy tasks, as the main thread is already running a tokio 0.2 Reactor and Timer.

Note that current_thread::Runtime does not implement Send itself and cannot be safely moved to other threads

Spawning from other threads

While current_thread::Runtime does not implement Send and cannot safely be moved to other threads, it provides a Handle that can be sent to other threads and allows to spawn new tasks from there.

For example:

use tokio_compat::runtime::current_thread::Runtime;
use std::thread;

let runtime = Runtime::new().unwrap();
let handle = runtime.handle();

thread::spawn(move || {
    // Spawn a `futures` 0.1 task on the other thread's runtime.
    let _ = handle.spawn(futures_01::future::lazy(|| {
        println!("hello from futures 0.1!");
        Ok(())
    }));

    // Spawn a `std::future` task on the other thread's runtime.
    let _ = handle.spawn_std(async {
        println!("hello from std::future!");
    });
}).join().unwrap();

Examples

Creating a new Runtime and running a future f until its completion and returning its result.

use tokio_compat::runtime::current_thread::Runtime;

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

// Use the runtime...
// runtime.block_on(f); // where f is a future

Structs

Builder(feature="rt-current-thread" or feature="rt-full") and feature="rt-current-thread"

Builds a single-threaded compatibility runtime with custom configuration values.

Handle(feature="rt-current-thread" or feature="rt-full") and feature="rt-current-thread"

Handle to spawn a future on the corresponding CurrentThread runtime instance

RunError(feature="rt-current-thread" or feature="rt-full") and feature="rt-current-thread"

Error returned by the run function.

Runtime(feature="rt-current-thread" or feature="rt-full") and feature="rt-current-thread"

Single-threaded runtime provides a way to start reactor and executor on the current thread.

TaskExecutor(feature="rt-current-thread" or feature="rt-full") and feature="rt-current-thread"

Executes futures on the current thread.

Functions

block_on_all(feature="rt-current-thread" or feature="rt-full") and feature="rt-current-thread"

Run the provided futures 0.1 future to completion using a runtime running on the current thread.

block_on_all_std(feature="rt-current-thread" or feature="rt-full") and feature="rt-current-thread"

Run the provided std::future future to completion using a runtime running on the current thread.

run(feature="rt-current-thread" or feature="rt-full") and feature="rt-current-thread"

Start a current-thread runtime using the supplied futures 0.1 future to bootstrap execution.

run_std(feature="rt-current-thread" or feature="rt-full") and feature="rt-current-thread"

Start a current-thread runtime using the supplied std::future ture to bootstrap execution.