Module tokio::runtime::current_thread
source · [−]Expand description
A runtime implementation that runs everything on the current thread.
current_thread::Runtime
is similar to the primary
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 Runtime
, the
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.
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::runtime::current_thread::Runtime;
use tokio::prelude::*;
use std::thread;
let mut runtime = Runtime::new().unwrap();
let handle = runtime.handle();
thread::spawn(move || {
handle.spawn(future::ok(()));
}).join().unwrap();
runtime.run().unwrap();
Examples
Creating a new Runtime
and running a future f
until its completion and
returning its result.
use tokio::runtime::current_thread::Runtime;
use tokio::prelude::*;
let mut runtime = Runtime::new().unwrap();
// Use the runtime...
// runtime.block_on(f); // where f is a future
Structs
Builds a Single-threaded runtime with custom configuration values.
Handle to spawn a future on the corresponding CurrentThread
runtime instance
Single-threaded runtime provides a way to start reactor and executor on the current thread.
Executes futures on the current thread.
Functions
Run the provided future to completion using a runtime running on the current thread.
Start a current-thread runtime using the supplied future to bootstrap execution.
Executes a future on the current thread.