pub trait TaskSpawner:
Send
+ Sync
+ Unpin
+ Debug
+ DynClone {
// Required methods
fn spawn(&self, fut: BoxFuture<'static, ()>) -> JoinHandle<()>;
fn spawn_critical(
&self,
name: &'static str,
fut: BoxFuture<'static, ()>,
) -> JoinHandle<()>;
fn spawn_blocking(&self, fut: BoxFuture<'static, ()>) -> JoinHandle<()>;
fn spawn_critical_blocking(
&self,
name: &'static str,
fut: BoxFuture<'static, ()>,
) -> JoinHandle<()>;
}
Expand description
A type that can spawn tasks.
The main purpose of this type is to abstract over TaskExecutor
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(Box::pin(async {
// -- snip --
}));
task.await.unwrap();
Use the TaskExecutor
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 manager = TaskManager::new(rt.handle().clone());
let executor = manager.executor();
let task = TaskSpawner::spawn(&executor, 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(&self, fut: BoxFuture<'static, ()>) -> JoinHandle<()>
fn spawn(&self, fut: BoxFuture<'static, ()>) -> JoinHandle<()>
Spawns the task onto the runtime.
See also [Handle::spawn
].
Sourcefn spawn_critical(
&self,
name: &'static str,
fut: BoxFuture<'static, ()>,
) -> JoinHandle<()>
fn spawn_critical( &self, name: &'static str, fut: BoxFuture<'static, ()>, ) -> JoinHandle<()>
This spawns a critical task onto the runtime.
Sourcefn spawn_blocking(&self, fut: BoxFuture<'static, ()>) -> JoinHandle<()>
fn spawn_blocking(&self, fut: BoxFuture<'static, ()>) -> JoinHandle<()>
Spawns a blocking task onto the runtime.
Sourcefn spawn_critical_blocking(
&self,
name: &'static str,
fut: BoxFuture<'static, ()>,
) -> JoinHandle<()>
fn spawn_critical_blocking( &self, name: &'static str, fut: BoxFuture<'static, ()>, ) -> JoinHandle<()>
This spawns a critical blocking task onto the runtime.