Function spawn

Source
pub fn spawn<T>(future: T) -> JoinHandle<T::Output> 
where T: Future + 'static, T::Output: 'static,
Expand description

Spawns a new asynchronous task, returning a JoinHandle for it.

Spawning a task enables the task to execute concurrently to other tasks. There is no guarantee that a spawned task will execute to completion. When a runtime is shutdown, all outstanding tasks are dropped, regardless of the lifecycle of that task.

§Examples

In this example, a server is started and spawn is used to start a new task that processes each received connection.

#[monoio::main]
async fn main() {
    let handle = monoio::spawn(async {
        println!("hello from a background task");
    });

    // Let the task complete
    handle.await;
}