pub struct TokioExecutor;Expand description
Executes callable tasks on Tokio’s blocking task pool.
TokioExecutor is a FutureExecutor: its Executor::call and
Executor::execute methods return a TokioExecution value that
implements Future with
Output = Result<R, E>. You
obtain the callable’s result by .awaiting (or polling) that future; it
is not a resolved Result at return time.
§Semantics
callschedules work immediately —Executor::callrunstokio::task::spawn_blockingsynchronously before it returns. A Tokio runtime must already be active on the current thread whencallruns (for example inside anasyncblock executed underRuntime::block_onor#[tokio::main]). Callingcallfirst and only then entering a runtime is wrong: the blocking task was submitted with no runtime atcalltime.- Any normal Tokio entry point works — you are not restricted to
Builder::new_current_thread; a multi-threadRuntimeor an async handler in a server is fine, as long ascallhappens while that runtime is running. - Await the returned future on Tokio — the
TokioExecutionpolls aJoinHandle; complete it with.awaitinside the same kind of Tokio-driven async context. - Blocking pool — the closure runs on Tokio’s blocking thread pool, not on the core async worker threads, so heavy synchronous work does not starve other async tasks on the runtime.
- Compared to
ThreadPerTaskExecutor— this type reuses Tokio-managed blocking threads (bounded pool) instead of one newstd::threadper task, and you await the result instead of calling a blockingTaskHandle::get.
§Examples
The following uses a single-thread Runtime only to keep the snippet
self-contained; #[tokio::main]
or a multi-thread runtime are equally valid.
use std::io;
use qubit_tokio_executor::{
Executor,
TokioExecutor,
};
tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()?
.block_on(async {
let executor = TokioExecutor;
let value = executor.call(|| Ok::<i32, io::Error>(40 + 2)).await?;
assert_eq!(value, 42);
Ok::<(), io::Error>(())
})?;Trait Implementations§
Source§impl Clone for TokioExecutor
impl Clone for TokioExecutor
Source§fn clone(&self) -> TokioExecutor
fn clone(&self) -> TokioExecutor
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for TokioExecutor
impl Debug for TokioExecutor
Source§impl Default for TokioExecutor
impl Default for TokioExecutor
Source§fn default() -> TokioExecutor
fn default() -> TokioExecutor
Source§impl Executor for TokioExecutor
impl Executor for TokioExecutor
Source§fn call<C, R, E>(&self, task: C) -> Self::Execution<R, E>
fn call<C, R, E>(&self, task: C) -> Self::Execution<R, E>
Spawns the callable on Tokio’s blocking task pool.
This method invokes tokio::task::spawn_blocking before returning.
A Tokio runtime must be active when this method runs; see TokioExecutor.
§Parameters
task- Callable to run on Tokio’s blocking task pool.
§Returns
A TokioExecution that implements Future with
Output = Result<R, E>. Await it to obtain the
callable’s result.