Skip to main content

BlockingSpawner

Trait BlockingSpawner 

Source
pub trait BlockingSpawner:
    Send
    + Sync
    + 'static {
    // Required method
    fn spawn_blocking_named(
        &self,
        name: Arc<str>,
        f: Box<dyn FnOnce() + Send + 'static>,
    ) -> JoinHandle<()>;
}
Expand description

Trait for spawning CPU-bound work on a supervised blocking thread pool.

Implementors register each spawned task in their supervision layer so it is visible to lifecycle management (snapshots, graceful shutdown, metrics). Callers that do not have a supervised spawner may fall back to tokio::task::spawn_blocking directly.

The trait is object-safe: it accepts a Box<dyn FnOnce() + Send + 'static> and returns a JoinHandle<()>. Callers that need a typed return value must communicate results through a channel or shared state.

§Examples

use std::sync::Arc;
use zeph_common::BlockingSpawner;

fn do_work(spawner: Arc<dyn BlockingSpawner>) {
    let handle = spawner.spawn_blocking_named(Arc::from("my_task"), Box::new(|| {
        // CPU-bound work
    }));
    // Caller can `.await` the handle.
    let _ = handle;
}

Required Methods§

Source

fn spawn_blocking_named( &self, name: Arc<str>, f: Box<dyn FnOnce() + Send + 'static>, ) -> JoinHandle<()>

Spawn a named blocking closure and return a JoinHandle<()> for completion.

Pass an Arc<str> for the task name — this avoids the need to leak memory when constructing dynamic task names. Static literals can be converted with Arc::from("my_task").

The implementation registers the task in its supervision layer before the closure begins executing. Results must be communicated via channels or shared state if needed.

If the closure panics, the implementation should log the error rather than propagating a panic to the caller. The returned JoinHandle<()> resolves to Ok(()) in all non-abort cases; it resolves to Err(JoinError) only if the bridge task itself is aborted externally.

Implementors§