[][src]Function tokio::executor::spawn

pub fn spawn<F>(f: F) -> Spawn where
    F: Future<Output = ()> + 'static + Send

Spawns a future on the default executor.

In order for a future to do work, it must be spawned on an executor. The spawn function is the easiest way to do this. It spawns a future on the default executor for the current execution context (tracked using a thread-local variable).

The default executor is usually a thread pool.

Examples

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

use tokio::net::TcpListener;

let mut listener = TcpListener::bind("127.0.0.1:8080").await?;

loop {
    let (socket, _) = listener.accept().await?;

    tokio::spawn(async move {
        // Process each socket concurrently.
        process(socket).await
    });
}

Panics

This function will panic if the default executor is not set or if spawning onto the default executor returns an error. To avoid the panic, use DefaultExecutor.