Почему try_ready! () С запросом hyper / reqwest никогда не заканчивается? - PullRequest
0 голосов
/ 01 мая 2019

И что использовать hyper / reqwest для получения результата API в impl for Future, но try_ready!() никогда не завершится.

Я пытался использовать hyper / reqwest с tokio::run() напрямую или использовать try_ready!()с чем-то, кроме гипер / запрос.Ни один из способов не зависает.

Например, приведенный ниже код печатает j0 и j1, но зависает на j2.

use futures::{try_ready, Async, Future, Poll};
use hyper::rt::Stream;
use hyper::{Client, Uri};
use serde_json::Value;

fn fetch() -> impl Future<Item = Value, Error = ()> {
    let client = Client::new();
    client
        .get(Uri::from_static("http://httpbin.org/ip"))
        .and_then(|res| res.into_body().concat2())
        .and_then(|body| Ok(serde_json::from_slice(&body).unwrap()))
        .map_err(|_| ())
}

fn test_fn() -> impl Future<Item = (), Error = ()> {
    fetch().and_then(|j| {
        println!("j0 {:?}", j);
        Ok(())
    })
}

struct Fetch;
impl Future for Fetch {
    type Item = Value;
    type Error = ();
    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
        Ok(Async::Ready(Value::default()))
    }
}

struct Test;
impl Future for Test {
    type Item = ();
    type Error = ();
    fn poll(&mut self) -> Result<Async<()>, ()> {
        let j = try_ready!(Fetch.poll());
        println!("j1 {:?}", j);

        let j = try_ready!(fetch().poll());
        println!("j2 {:?}", j);

        Ok(Async::Ready(()))
    }
}

fn main() {
    println!("start");
    tokio::run(test_fn());
    tokio::run(Test);
}

...