Hyper Client:在一个 tokio 任务中执行 Http 请求,在另一个任务中读出http_info

Hyper Client: Perform Http request in one tokio task, read out http_info in another task

提问人:bokov 提问时间:11/8/2023 更新时间:11/8/2023 访问量:26

问:

请考虑以下最小示例:

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

但是,内存管理不允许我这样做。

有谁知道如何解决这个问题?

锈-tokio hyper

评论

0赞 Chayim Friedman 11/9/2023
我不认为请求有,只有响应有。HttpInfo

答: 暂无答案