期货::选择有什么区别!和 tokio::select?

编程入门 行业动态 更新时间:2024-10-28 11:32:19
本文介绍了期货::选择有什么区别!和 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?

本文发布于:2023-11-25 07:43:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1628906.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有什么区别   期货   select   tokio

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!