logo

Function actix_rt::spawn

source · []
pub fn spawn<Fut>(f: Fut) -> JoinHandle<Fut::Output>Notable traits for JoinHandle<T>impl<T> Future for JoinHandle<T>    type Output = Result<T, JoinError>; where
    Fut: Future + 'static,
    Fut::Output: 'static, 
Expand description

Spawns a future on the current thread as a new task.

If not immediately awaited, the task can be cancelled using JoinHandle::abort.

The provided future is spawned as a new task; therefore, panics are caught.

Panics

Panics if Actix system is not running.

Examples

// task resolves successfully
assert_eq!(actix_rt::spawn(async { 1 }).await.unwrap(), 1);

// task panics
assert!(actix_rt::spawn(async {
    panic!("panic is caught at task boundary");
})
.await
.unwrap_err()
.is_panic());

// task is cancelled before completion
let handle = actix_rt::spawn(actix_rt::time::sleep(Duration::from_secs(100)));
handle.abort();
assert!(handle.await.unwrap_err().is_cancelled());