reth_tasks

Trait TaskSpawner

source
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§

source

fn spawn(&self, fut: BoxFuture<'static, ()>) -> JoinHandle<()>

Spawns the task onto the runtime. See also [Handle::spawn].

source

fn spawn_critical( &self, name: &'static str, fut: BoxFuture<'static, ()>, ) -> JoinHandle<()>

This spawns a critical task onto the runtime.

source

fn spawn_blocking(&self, fut: BoxFuture<'static, ()>) -> JoinHandle<()>

Spawns a blocking task onto the runtime.

source

fn spawn_critical_blocking( &self, name: &'static str, fut: BoxFuture<'static, ()>, ) -> JoinHandle<()>

This spawns a critical blocking task onto the runtime.

Implementations on Foreign Types§

source§

impl<'a, T: 'a + TaskSpawner + ?Sized> TaskSpawner for &'a T
where &'a T: Send + Sync + Unpin + Debug + DynClone,

source§

fn spawn(&self, fut: BoxFuture<'static, ()>) -> JoinHandle<()>

source§

fn spawn_critical( &self, name: &'static str, fut: BoxFuture<'static, ()>, ) -> JoinHandle<()>

source§

fn spawn_blocking(&self, fut: BoxFuture<'static, ()>) -> JoinHandle<()>

source§

fn spawn_critical_blocking( &self, name: &'static str, fut: BoxFuture<'static, ()>, ) -> JoinHandle<()>

source§

impl<T: TaskSpawner + ?Sized> TaskSpawner for Arc<T>
where Arc<T>: Send + Sync + Unpin + Debug + DynClone,

source§

fn spawn(&self, fut: BoxFuture<'static, ()>) -> JoinHandle<()>

source§

fn spawn_critical( &self, name: &'static str, fut: BoxFuture<'static, ()>, ) -> JoinHandle<()>

source§

fn spawn_blocking(&self, fut: BoxFuture<'static, ()>) -> JoinHandle<()>

source§

fn spawn_critical_blocking( &self, name: &'static str, fut: BoxFuture<'static, ()>, ) -> JoinHandle<()>

Implementors§