【发布时间】:2019-10-07 06:39:29
【问题描述】:
我决定使用 hyper create 来构建一个读取 POST 方法的主体数据的服务器。 How do I synchronously return a value calculated in an asynchronous Future in stable Rust? 解释了我正在寻找的部分内容,但我不想使用tokio::run 或future::lazy,因为根据我的理解,超级使用 Tokio 和期货,超级主体返回一个流。我想要完成的是找到其他处理流的方法,并获得有关超Request 方法的更多知识。
在第一种方法中,我concat2 然后调用wait。 wait 阻塞当前线程,所以我的代码挂起。
if Method::POST == req.method() {
let body = req.into_body().concat2().wait();
// convert to json and do something with the data.
Ok(Response::new(Body::from("OK: data submitted")))
}
在第二种方法中,我尝试使用poll 和and_then,但我总是得到NotReady。结果类型为futures::poll::Async<hyper::Chunk>。
if Method::POST == req.method() {
let body = req.into_body().concat2().poll().and_then(|e| {
// do something
Ok(e)
});
match body {
Ok(e) => println!("{:#?}", e),
Err(r) => println!("{:#?}", r),
};
Ok(Response::new(Body::from("")))
}
- 如何解除对当前线程的阻塞并返回结果?
- 如何轮询然后返回结果?
如果可能,请说明如何处理futures::poll::Async 和wait() 的良好做法。目前,async/await 在 Rust 中是不稳定的,所以我不能使用它。
【问题讨论】:
-
看来How do I synchronously return a value calculated in an asynchronous Future in stable Rust? 的答案可能会回答您的问题。如果没有,请edit您的问题来解释差异。否则,我们可以将此问题标记为已回答。