Why does inner_task
run at all when nothing calls .await
on it? I'm assuming it's because .await
was called on the outer task, but I can't find any relevant docs explaining what is happening.
use tokio;
async fn test() {
println!("Hello from test");
}
#[tokio::main]
async fn main() {
let (tx, mut rx) = tokio::sync::mpsc::channel(32);
let task = tokio::task::spawn(async move {
println!("Hello from outer task");
let inner_task = tokio::task::spawn(async move {
println!("Hello from inner task");
});
println!("Outer task sending inner task");
tx.send(inner_task).await.unwrap();
println!("Goodbye from outer task");
});
println!("Have not awaited task yet");
let t = test();
println!("Have not awaited test yet");
t.await;
println!("Test was awaited");
println!("Outer task not yet awaited");
task.await;
println!("Outer task was awaited");
while let Some(t) = rx.recv().await {
println!("not awaiting inner task");
}
}
Output:
Have not awaited task yet
Have not awaited test yet
Hello from test
Test was awaited
Outer task not yet awaited
Hello from outer task
Outer task sending inner task
Goodbye from outer task
Outer task was awaited
not awaiting inner task
Hello from inner task