当运行以下代码时:
我的 cargo.toml 文件如下所示:
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
我主函数上已经添加了tokio注解,如预期一样:
#[tokio::main]
async fn main() {
...
我的 cargo.toml 文件如下所示:
[dependencies]
...
futures = { version="0", features=["executor"] }
tokio = "1"
我的cargo.lock文件显示,我只有futures和tokio的一个版本(分别是"1.2.0"和"0.3.12")。
这已经涵盖了我在其他地方找到的有关此问题的所有解释。有任何想法吗?谢谢。
async fn main
,那么future.await
就足够了,你不需要来自futures-rs的额外执行器。 - Ömer Erdenasync fn main
,这意味着你的主线程将被 tokio 执行器用于轮询。你可以随时创建一个新线程来处理非异步上下文。 - Ömer Erden