Tokio::select with spawn'ed tasks

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?

The only difference is that the first version does not unwrap the Result returned by awaiting the JoinHandle, silently ignoring any errors.

In general, using .await on a JoinHandle (which select! does implicitly) does not affect the task in any way.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.