期货::选择有什么区别!和 tokio::select? [英] What is the difference between futures::select! and tokio::select?

查看:63
本文介绍了期货::选择有什么区别!和 tokio::select?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Tokio,我想从两个不同的 mpsc 队列接收请求.select! 似乎是要走的路,但我不确定 futures::select!tokio::select! 之间有什么区别代码>.在哪种情况下,您应该使用一种而不是另一种?

解决方案

tokio::select! 是基于 futures::select! 的经验构建的,但改进了稍微调整一下以使其更符合人体工程学.例如.futures-rs 版本的 select! 需要 Futures 来实现 FusedFuture,而 Tokio 的版本不再需要这个.

与此相反,Tokio 的版本支持宏中的先决条件以涵盖相同的用例.

tokio 存储库中的 PR 对此进行了更多阐述.

此更改也是 为 futures-rs 版本提出的,但目前尚未在那里实施.

如果您的项目中已经包含 Tokio,那么使用 Tokio 的版本似乎更可取.但是,如果您没有并且不想添加额外的依赖项,那么 futures-rs 版本也将以几乎相同的方式涵盖大多数用例.主要区别在于某些Future可能需要通过FutureExt::fuse()扩展方法转换为FusedFuture.>

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?

解决方案

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.

这篇关于期货::选择有什么区别!和 tokio::select?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆