【发布时间】:2021-02-23 06:26:20
【问题描述】:
当运行这样的代码时:
use futures::executor;
...
pub fn store_temporary_password(email: &str, password: &str) -> Result<(), Box<dyn Error>> {
let client = DynamoDbClient::new(Region::ApSoutheast2);
...
let future = client.put_item(input);
executor::block_on(future)?; <- crashes here
Ok(())
}
我得到错误:
thread '<unnamed>' panicked at 'there is no reactor running, must be called from the context of a Tokio 1.x runtime
我的 main 应该有 tokio 注释:
#[tokio::main]
async fn main() {
...
我的 cargo.toml 看起来像:
[dependencies]
...
futures = { version="0", features=["executor"] }
tokio = "1"
我的 cargo.lock 显示我只有一个版本的期货和 tokio(分别为“1.2.0”和“0.3.12”)。
这用尽了我在其他地方找到的关于这个问题的解释。有任何想法吗?谢谢。
【问题讨论】:
-
You shouldn't block the thread where async operations meant to poll。如果您将
async fn main与 tokio 执行器一起使用,future.await应该就足够了,您不需要来自 futures-rs 的额外执行器。 -
谢谢,但是我必须使我的代码库中的每个函数都异步吗?如果只有异步函数可以“等待”其他异步函数,那么我似乎必须彻底重构所有内容?我错过了什么吗?
-
@Chris 是的,等待其他未来的每个函数都应该是异步的,否则您将执行阻塞操作。
-
再次感谢 :) 这样做的惯用方法是什么?人们真的会因为调用堆栈中的一个函数想要进行网络调用而最终将他们的大部分函数更改为“异步”吗?诚实的问题,我不确定,但对我来说似乎很沉重。 :)
-
@Chris 我必须让我的代码库中的每个函数都异步? 抱歉,我以为我编辑了我之前的评论,如果您使用的是
asyn fn main,这意味着您的主线程将用于 tokio executor 的轮询。你总是可以创建一个新线程来处理你的非异步上下文
标签: rust rust-tokio