pub trait TaskSpawner:
Send
+ Sync
+ Unpin
+ Debug
+ DynClone {
// Required methods
fn spawn_task(
&self,
fut: Pin<Box<dyn Future<Output = ()> + Send>>,
) -> JoinHandle<()> ⓘ;
fn spawn_critical_task(
&self,
name: &'static str,
fut: Pin<Box<dyn Future<Output = ()> + Send>>,
) -> JoinHandle<()> ⓘ;
fn spawn_blocking_task(
&self,
fut: Pin<Box<dyn Future<Output = ()> + Send>>,
) -> JoinHandle<()> ⓘ;
fn spawn_critical_blocking_task(
&self,
name: &'static str,
fut: Pin<Box<dyn Future<Output = ()> + Send>>,
) -> JoinHandle<()> ⓘ;
}Available on crate feature
tasks only.Expand description
A type that can spawn tasks.
The main purpose of this type is to abstract over Runtime so it’s more convenient to
provide default impls for testing.
§Examples
Use the TokioTaskExecutor that spawns with [tokio::task::spawn]
use reth_tasks::{TaskSpawner, TokioTaskExecutor};
let executor = TokioTaskExecutor::default();
let task = executor.spawn_task(Box::pin(async {
// -- snip --
}));
task.await.unwrap();Use the Runtime that spawns task directly onto the tokio runtime via the [Handle].
fn t() {
use reth_tasks::TaskSpawner;
let rt = tokio::runtime::Runtime::new().unwrap();
let runtime = Runtime::with_existing_handle(rt.handle().clone()).unwrap();
let task = TaskSpawner::spawn_task(&runtime, Box::pin(async {
// -- snip --
}));
rt.block_on(task).unwrap();The TaskSpawner trait is DynClone so Box<dyn TaskSpawner> are also Clone.
Required Methods§
Sourcefn spawn_task(
&self,
fut: Pin<Box<dyn Future<Output = ()> + Send>>,
) -> JoinHandle<()> ⓘ
fn spawn_task( &self, fut: Pin<Box<dyn Future<Output = ()> + Send>>, ) -> JoinHandle<()> ⓘ
Spawns the task onto the runtime.
See also [Handle::spawn].
Sourcefn spawn_critical_task(
&self,
name: &'static str,
fut: Pin<Box<dyn Future<Output = ()> + Send>>,
) -> JoinHandle<()> ⓘ
fn spawn_critical_task( &self, name: &'static str, fut: Pin<Box<dyn Future<Output = ()> + Send>>, ) -> JoinHandle<()> ⓘ
This spawns a critical task onto the runtime.
Trait Implementations§
Source§impl<'clone> Clone for Box<dyn TaskSpawner + 'clone>
impl<'clone> Clone for Box<dyn TaskSpawner + 'clone>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read more