In my application I have two tasks dealing with a websocket.
let read_task = tokio::spawn(async move {
// Read from websocket
});
let write_task = tokio::spawn(async move {
// Write to websocket
});
Is there a big difference between the following two snippets?
First:
tokio::select! {
_ = read_task => {
println!("Read task completed first");
}
_ = write_task => {
println!("Write task completed first");
}
}
Second:
tokio::select! {
_ = async {
read_task.await.unwrap();
} => {
println!("Read task completed first");
}
_ = async {
write_task.await.unwrap();
} => {
println!("Write task completed first");
}
}
I am slightly confused because, as I understand, tokio::select!
runs all branches concurrently on the same task. And in my case each branch is a tokio::spawn
.
Because of this, is one approach more performant than the other? As in, is select!
hindering the execution of spawn
-ed tasks? Could you please clarify?