-
If I comment out those three lines of code, the program exits early and does not print out the full result. What I'm asking is, is there a best practice for when I should #[tokio::main]
async fn main() {
let mut handles = Vec::with_capacity(10);
for i in 0..10 {
handles.push(tokio::spawn(my_bg_task(i)));
}
// 🔺🔺🔺
for handle in handles {
handle.await.unwrap();
}
// 🔺🔺🔺
}
async fn my_bg_task(i: u64) {
println!("{}",i)
} |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
When you return from |
Beta Was this translation helpful? Give feedback.
-
I have some new understanding of this problem. This actually involves the shutdown of runtime. What happens when a runtime is dropped can be found at this link. |
Beta Was this translation helpful? Give feedback.
When you return from
main
, the runtime will start shutting down, which includes killing all other spawned tasks. You should await tasks when you need to wait for them to finish.