英文:
Rust equivalent of Python Future (i.e. with set_result() method)
问题 {#heading}
在一些知名的实用程序包中,是否有一个类似于Python的asyncio.Future
的结构体,即具有.set_result(value)
方法,并且会一直处于挂起状态,直到某个其他任务调用该方法,此时它将解析为该值?
我曾通过阅读有关Rust futures工作原理的文档来重新创建这个功能,但我对此还有些陌生,我的代码有点笨拙,可能有一些错误,并且有一些我认为是不必要的限制,但编译器不足够聪明,也没有我聪明。我相信我以前在一个公共的crate中偶然发现了类似的功能,但我不记得它叫什么名字。 英文:
Is there a struct in some well-known utilities crate that does what Python's asyncio.Future
does, i.e has a .set_result(value)
method, and will be pending indefinitely until some other task calls that method, at which point it resolves to that value?
I've recreated this functionality by reading the docs on how Rust futures work but I'm still a bit new to this and my code is a bit clunky and likely buggy and has some restrictions I believe are unnecessary, but the compiler isn't smart enough to prove otherwise and neither am I. I believe I stumbled across similar functionality in a public crate a while back but I can't remember what it was called.
答案1 {#1}
得分: 2
这听起来像是一个异步的一次性通道,即接收者-发送者配对,其中接收者可以等待,直到值被推送到发送者。如果您想以与运行时无关的方式执行此操作,您可能需要 futures::channel::oneshot
。
英文:
This sounds like a asynchronous oneshot channel, i.e. the receiver-sender pair, where receiver can be awaited until the value is pushed into the sender. If you want to do this in a runtime-agnostic way, you probably want futures::channel::oneshot
.