I'm using Tokio and I want to receive requests from two different mpsc
queues. select!
seems like the way to go, but I'm not sure what the difference is between futures::select!
and tokio::select!
. Under which circumstances one should you use one over the other?
The tokio::select! macro allows waiting on multiple async computations and returns when a single computation completes. Two oneshot channels are used. Either channel could complete first.
Tokio is an asynchronous runtime for the Rust programming language. It provides the building blocks needed for writing network applications. It gives the flexibility to target a wide range of systems, from large servers with dozens of cores to small embedded devices. Get Started.
Futures in Rust are analogous to promises in JavaScript. They are a powerful abstraction over the concurrency primitives available in Rust. They are also a stepping stone to async/await, which allows users to write asynchronous code that looks like synchronous code.
tokio::select! The tokio::select! macro allows waiting on multiple async computations and returns when a single computation completes. Two oneshot channels are used.
Select is more formal than choose, and in everyday English, people usually say choose rather than select. Also there's a homophone for choose which is chews. According to Merriam-Webster. choose: to have a preference for or to select freely and after consideration.
Options vs. Futures: An Overview. Options and futures are both financial products that investors use to make money or to hedge current investments. Both are agreements to buy an investment at a specific price by a specific date.
Both tokio::spawn and select! enable running concurrent asynchronous operations. However, the strategy used to run concurrent operations differs. The tokio::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.
tokio::select!
was built out of experiences with futures::select!
, but improves a bit on it to make it more ergonomic. E.g. the futures-rs
version of select!
requires Future
s to implement FusedFuture
, whereas Tokio's version no longer requires this.
Instead of this, Tokio's version supports preconditions in the macro to cover the same use-cases.
The PR in the tokio repo elaborates a bit more on this.
This change was also proposed for the futures-rs version, but has not been implemented there so far.
If you already have Tokio included in your project, then using Tokio's version seems preferable. But if you have not and do not want to add an additional dependency, then the futures-rs version will cover most use-cases too in a nearly identical fashion. The main difference is that some Future
s might need to be converted into FusedFuture
s through the FutureExt::fuse()
extension method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With