Expand description
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 listener = TcpListener::bind(&addr).unwrap();
let server = listener.incoming()
.map_err(|e| println!("error = {:?}", e))
.for_each(|socket| {
tokio::spawn(process(socket))
});
tokio::run(server);
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
.