Function tokio::task::spawn_local
source · [−]pub fn spawn_local<F>(future: F) -> JoinHandle<F::Output>ⓘNotable traits for JoinHandle<T>impl<T> Future for JoinHandle<T> type Output = Result<T, JoinError>;
where
F: Future + 'static,
F::Output: 'static,
Expand description
Spawns a !Send
future on the current LocalSet
.
The spawned future will run on the same thread that called spawn_local
.
You do not have to .await
the returned JoinHandle
to make the
provided future start execution. It will start running in the background
immediately when spawn_local
is called.
Panics
This function panics if called outside of a LocalSet
.
Note that if tokio::spawn
is used from within a LocalSet
, the
resulting new task will not be inside the LocalSet
, so you must use
use spawn_local
if you want to stay within the LocalSet
.
Examples
use std::rc::Rc;
use tokio::task;
#[tokio::main]
async fn main() {
let unsend_data = Rc::new("my unsend data...");
let local = task::LocalSet::new();
// Run the local task set.
local.run_until(async move {
let unsend_data = unsend_data.clone();
task::spawn_local(async move {
println!("{}", unsend_data);
// ...
}).await.unwrap();
}).await;
}