Rust Hyper:如何取消阻止将来的线程并返回结果

问题描述 投票:0回答:1

我是锈病新手,所以我决定使用超级创建来构建服务器。我查看了超级文档,并决定尝试一些新的东西,以便对其他超级方法的工作原理有一个广泛的了解。我正在尝试读取Post方法的主体数据。

在第一种方法中,我concat2然后调用wait,现在调用wait,阻塞当前线程,使我的代码挂起。

在第二种方法中,我尝试使用polland_then,但是我总是得到NotReady,返回结果类型为futures :: poll :: Async] >>>

第一种方法

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")))
}....

第二种方法

 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("")))
 }.....

问题1

:如何解除对当前线程的阻止并返回结果?

问题2

:如何轮询然后返回结果?

如果可能,请您说明如何处理futures :: poll :: Async

wait()的良好做法。目前异步/等待结果到稳定Rust中的[[异步fn不稳定,所以我不能使用它。我是锈病新手,所以我决定使用超级创建来构建服务器。我查看了超级文档,并决定尝试一些新的东西,以便对其他超级...... >>>

[我确定您已经发现,在期货上调用wait()

是一种反模式

并破坏了整体异步IO的目的(因为您正在阻塞线程,从而使执行者处于阻塞状态)这样)。hyper路由接受实现IntoFuture的返回值,该返回值以覆盖方式为IntoFutureOption<T>实现。因此,您实际编写的内容根本不需要阻止-您只需要像这样组合期货:

Result<T, E>

您甚至没有

使用代码中的if Method::POST == req.method() { req.into_body().concat2().map(|_body| { Response::new(Body::from("OK: data submitted")) }) } 内容,但我假设这是出于MCVE的目的。在body组合器内部,该值当前显示为map(),并允许您使用它进行任何操作。

rust hyper
1个回答
0
投票
[我确定您已经发现,在期货上调用wait()

是一种反模式

© www.soinside.com 2019 - 2024. All rights reserved.