TL;DR;
Tokio::spawn
: new task, different threads, no borrowing.select!
: same task/thread, borrowing.
Both
tokio::spawn
andselect!
enable running concurrent asynchronous operations. However, the strategy used to run concurrent operations differs. Thetokio::spawn
function takes an asynchronous operation and spawns a new task to run it. A task is the object that the Tokio runtime schedules. Two different tasks are scheduled independently by Tokio. They may run simultaneously on different operating system threads. Because of this, a spawned task has the same restriction as a spawned thread: no borrowing.The
select!
macro runs all branches concurrently on the same task. Because all branches of theselect!
macro are executed on the same task, they will never run simultaneously. Theselect!
macro multiplexes asynchronous operations on a single task.
Reference: Select