提问人:bokov 提问时间:11/8/2023 更新时间:11/8/2023 访问量:26
Hyper Client:在一个 tokio 任务中执行 Http 请求,在另一个任务中读出http_info
Hyper Client: Perform Http request in one tokio task, read out http_info in another task
问:
请考虑以下最小示例:
use http::{Method, Request, Uri};
use hyper::{client::connect::HttpInfo, Body, Client};
#[tokio::main]
async fn main() -> Result<(), String> {
let req = Request::builder()
.method(Method::POST)
.uri(Uri::from_static("http://foo.bar"))
.body(Body::from("a lot of data..."))
.unwrap();
let hyper_client = Client::new();
tokio::spawn(async move {
let resp = hyper_client.request(req).await;
});
tokio::spawn(async move {
let http_info = req.extensions().get::<HttpInfo>().unwrap();
});
Ok(())
}
由于以下错误,它无法编译:
use of moved value: `req`
value used here after move
tmp.rs(14, 18): value moved here
tmp.rs(15, 41): variable moved due to use in generator
tmp.rs(19, 25): use occurs due to use in generator
tmp.rs(6, 9): move occurs because `req` has type `http::Request<Body>`, which does not implement the `Copy` trait
我希望有可能从不同的tookio任务中读出与执行HTTP请求的任务。http_info
但是,内存管理不允许我这样做。
有谁知道如何解决这个问题?
答: 暂无答案
评论
HttpInfo