Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between futures::select! and tokio::select?

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?

like image 627
ynimous Avatar asked Mar 23 '20 10:03

ynimous


People also ask

How does Tokio select work?

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.

What is Tokio runtime?

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.

What is a future in Rust?

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.

What is the use of Tokio select?

tokio::select! The tokio::select! macro allows waiting on multiple async computations and returns when a single computation completes. Two oneshot channels are used.

What is the difference between choose and select?

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.

What is the difference between options and futures?

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.

What is the difference between Tokio::spawn and select ()?

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.


Video Answer


1 Answers

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 Futures 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 Futures might need to be converted into FusedFutures through the FutureExt::fuse() extension method.

like image 103
Matthias247 Avatar answered Oct 16 '22 20:10

Matthias247