4

我正在尝试将期货附加到BufferUnordered. 目前我将它们直接推送到底层流中,FuseBufferUnordered是空的,所以推送到它没有效果,下面的循环没有收到第三个响应。next将(1)的定义更改为stream.buffer_unordered(1)似乎可以使其工作,因为基础流不是空的/已完成。

extern crate url;
extern crate futures;
extern crate tokio_core;
extern crate reqwest;

use url::Url;
use futures::*;
use tokio_core::reactor::Core;
use reqwest::unstable::async::{Client, Response, Decoder};

fn main() {
    let mut core = Core::new().unwrap();
    let client = Client::new(&core.handle()).unwrap();
    let hyper = client.get("https://hyper.rs").unwrap().send();
    let google = client.get("https://google.com").unwrap().send();
    let stream = stream::futures_unordered(vec![future::ok(hyper), future::ok(google)]);

    let mut next = stream.buffer_unordered(5).into_future(); // (1)
    loop {
        match core.run(next) {

            Ok((None, _something)) => {
                println!("finished");
                break;
            },
            Ok((Some(response), mut next_requests)) => {
                {
                    let inner = next_requests.get_mut();
                    println!("{}", inner.is_empty());
                    println!("{}", response.status());
                    let yahoo = client.get("https://yahoo.com").unwrap().send();
                    inner.push(future::ok(yahoo)); // no effect here
                }
                next = next_requests.into_future();
            }
            Err((error, next_requests)) => {
                next = next_requests.into_future();
            }
        }
    }
}

如何添加更多期货BufferUnordered?我真的必须把它锁起来或按照这些思路做一些事情吗?

4

0 回答 0