Skip to main content

spawn

Function spawn 

Source
pub fn spawn<F>(name: &'static str, future: F) -> TaskHandle<F::Output> ⓘ
where F: Future + Send + 'static, F::Output: Send + 'static,
Expand description

Spawn future as a task named name on the current tokio runtime.

Returns without awaiting or waiting on the task, so a non-blocking path may call it; tokio takes a brief internal lock to queue the task. A panic in the task is logged at ERROR with name.

§Panics

When called outside a tokio runtime, as tokio::spawn does.

§Examples

let handle = spate_core::task::spawn("answer", async { 6 * 7 });
assert_eq!(handle.await.unwrap(), 42);

// Dropping a handle aborts its task.
drop(spate_core::task::spawn("forever", std::future::pending::<()>()));