-
` #[tokio::test] } Why does the task2 execute without await? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
This is the difference between This means that this: let mut handles = Vec::new();
for i in num_tasks {
handles.push(tokio::spawn(start_task(i)));
}
let mut outputs = Vec::new();
for handle in handles {
outputs.push(handle.await.unwrap());
} will execute all |
Beta Was this translation helpful? Give feedback.
This is the difference between
tokio::spawn
and other futures. With most normal futures, they don't execute until you.await
them, buttokio::spawn
is different: the task starts executing in the background immediately, and using.await
on the returnedJoinHandle
is just a way to wait for it to finish — it doesn't affect the execution of the task.This means that this:
will execute all
num_tasks
tasks in parallel, even though only oneJoinHandle
is.await
ed at the time.