【发布时间】:2021-01-21 23:55:52
【问题描述】:
我有以下问题:
我正在尝试从async 函数调用sync 闭包,但sync closure 必须稍后调用另一个async 函数。
我无法进行异步关闭,因为它们目前不稳定:
error[E0658]: async closures are unstable
所以我必须以某种方式这样做。
我发现了一些与该问题相关的问题,例如this,但是当我尝试实现它时,我收到以下错误:
Cannot start a runtime from within a runtime.
This happens because a function (like `block_on`)
attempted to block the current thread while the
thread is being used to drive asynchronous tasks.'
这里是playground link,希望可以显示我遇到的问题。
我正在使用标题中所述的 tokio。
【问题讨论】:
-
您可以创建一个返回异步块的闭包 -
|| async { ... }而不是async || { ... }。 -
详细说明,这是您的游乐场,建议进行更改:play.rust-lang.org/…
-
@Cerberus 非常感谢您,现在看来是要走的路了。只是为了将来的读者,我现在要发表评论,因为闭包返回异步块(而不是像以前那样正常)闭包应该用 .awat 调用
标签: asynchronous rust rust-tokio