如何同时使用 actix-web 3 和 rusoto 0.46?

Mat*_*rts 7 rust rusoto actix-web

当我尝试同时使用 actix-web 3 和 rusoto 0.46 时,出现以下运行时错误:

thread 'actix-rt:worker:0' panicked at 'there is no reactor running, must be called from the context of a Tokio 1.x runtime', /Users/matt/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.2.0/src/runtime/blocking/pool.rs:85:33
Run Code Online (Sandbox Code Playgroud)

小可复制:

use actix_web::{get, App, HttpResponse, HttpServer, Responder}; // 3
use rusoto_core::Region; // 0.46 
use rusoto_dynamodb::{DynamoDb, DynamoDbClient, ListTablesInput};
use std::default::Default;

#[get("/tables")]
async fn tables(_req_body: String) -> impl Responder {
    let client = DynamoDbClient::new(Region::default());
    let list_tables_input: ListTablesInput = Default::default();
    match client.list_tables(list_tables_input).await {
        Ok(_output) => HttpResponse::Ok().finish(),
        Err(_error) => HttpResponse::InternalServerError().finish(),
    }
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    HttpServer::new(|| App::new().service(tables))
        .bind("0.0.0.0:8080")?
        .run()
        .await
}
Run Code Online (Sandbox Code Playgroud)

随附货物文件:

[package]
name = "hello_actix_rusoto"
version = "0.1.0"
authors = ["Matt Roberts <mattroberts297@users.noreply.github.com>"]
edition = "2018"

[dependencies]
rusoto_core = "0.46"
rusoto_dynamodb = "0.46"
actix-web = "3"
Run Code Online (Sandbox Code Playgroud)

这是一个带有完整代码的非常小的 GitHub 存储库链接,这里是重现错误的方法:

[package]
name = "hello_actix_rusoto"
version = "0.1.0"
authors = ["Matt Roberts <mattroberts297@users.noreply.github.com>"]
edition = "2018"

[dependencies]
rusoto_core = "0.46"
rusoto_dynamodb = "0.46"
actix-web = "3"
Run Code Online (Sandbox Code Playgroud)

Ibr*_*med 9

rusotov0.46 取决于tokiov1.0。actix-web但是,v3 仍在使用tokiov0.2。这两个版本不向后兼容,因此出现错误消息。要解决此问题,您可以升级到更新版本的 actix-web:

actix-web = "4.0.0-beta.8"
Run Code Online (Sandbox Code Playgroud)

或者您可以使用tokio-compat2兼容层。这需要在任何不兼容的.await调用前加上.compat()

actix-web = "4.0.0-beta.8"
Run Code Online (Sandbox Code Playgroud)

或者您可以降级rusoto到使用tokiov0.2的旧版本:

rusoto_core = "0.43"
Run Code Online (Sandbox Code Playgroud)

  • 谢谢@ibraheem-ahmed。将 `rusoto_core` 和 `rusoto_dynamo` 降级至 0.43 有效。我已经让它与sync()和tokio Core一起工作。我只需要将核心重构为 actix 应用程序状态,然后编辑您的答案以包含解决方案(前提是您对此感到满意)。也感谢升级路径的解决方法。再次感谢你。 (2认同)