How can I perform parallel asynchronous HTTP GET requests with reqwest?

人走茶凉 提交于 2019-11-30 21:17:34

As of reqwest 0.9:

use futures::{stream, Future, Stream}; // 0.1.26
use reqwest::r#async::Client; // 0.9.14
use tokio; // 0.1.18

type Result<T> = std::result::Result<T, Box<std::error::Error>>;

const PARALLEL_REQUESTS: usize = 2;

fn main() -> Result<()> {
    let client = Client::new();

    let urls = vec!["https://api.ipify.org", "https://api.ipify.org"];

    let bodies = stream::iter_ok(urls)
        .map(move |url| {
            client
                .get(url)
                .send()
                .and_then(|res| res.into_body().concat2().from_err())
        })
        .buffer_unordered(PARALLEL_REQUESTS);

    let work = bodies
        .for_each(|b| {
            println!("Got {} bytes", b.len());
            Ok(())
        })
        .map_err(|e| panic!("Error while processing: {}", e));

    tokio::run(work);
    Ok(())
}

stream::iter_ok(urls)

stream::iter_ok

Take a collection of strings and convert it into a Stream.

.and_then(|res| res.into_body().concat2().from_err())

Stream::concat2, Stream::from_err

Take each response's body stream and collect it all into one big chunk.

.buffer_unordered(N);

Stream::buffer_unordered

Convert a stream of futures into a stream of those future's values, executing the futures in parallel.

let work = bodies.for_each(|b| {
    println!("Got {} bytes", b.len());
    Ok(())
});

Stream::for_each

Convert the stream back into a future.

See also:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!