【发布时间】:2019-10-10 13:38:01
【问题描述】:
我正在使用 Rust 2018 Stable 和 Actix-Web 编写 Web 服务。使用 Reqwest,我正在从一个路由处理程序函数中向不同的站点发出 HTTP 请求。简单来说就是这个样子
extern crate reqwest;
use actix_web;
use reqwest::Url;
pub fn testing(req: actix_web::HttpRequest) -> actix_web::Result<actix_web::HttpResponse> {
println!(">>> testing request begin");
let url = Url::parse("https://example.com/").unwrap();
println!(">>> testing url built");
let req = reqwest::Client::new().post(url);
println!(">>> testing req prepared");
let res_struct = req.send();
println!(">>> testing res_struct received");
let res = res_struct.unwrap();
println!(">>> testing res unwrapped");
Ok(format!("done.").into())
}
这不起作用,我收到以下错误消息(错误打印了 8 次,“worker:1”到“worker:8”,尽管只调用了一次函数):
thread 'actix-rt:worker:1' panicked at 'called `Result::unwrap()`
on an `Err` value: Error(BlockingClientInFutureContext,
"https://www.example.com/")', src/libcore/result.rs:999:5
Panic in Arbiter thread, shutting down system.
Google 在 “BlockingClientInFutureContext” 上没有发现任何有用的信息,但我猜它与 async/await 或 Tokio 自己的未来有某种关系?
感谢您提供有关阅读内容的任何指示。另外,我是 Rust 新手。
处理函数从 Actix-Web HTttpServer 调用:
HttpServer::new(|| App::new().service(
web::resource("/testing").route(
web::get().to(views::testing)
)
)).bind("127.0.0.1:8001")?.run()
【问题讨论】:
-
我认为 tokio 或 async/await 不会调用 'unwrap()'。在您的代码中:
.post(Url::parse(API_ENDPOINT).unwrap()),您正在调用 unwrap(),如果 unwrap() 失败,这将导致恐慌。您是否尝试过将其更改为不使用 unwrap() 的调用? -
感谢您的想法,我更新了问题。展开 reqwest 响应时它会发生恐慌。
-
我认为如果您可以提交带有主 fn 的 minimal reproducible example 会有所帮助,以便成员可以将您的示例复制并粘贴到他们的 IDE 中。
-
我用一个完整的例子更新了这个问题。几乎可以肯定这可以通过
actix_web::web::block()以某种方式解决。