rust reqwest異步併發處理


use futures;
use futures::future;
use futures::stream::{self, StreamExt};
#[tokio::main]
async fn main(){

    let paths = vec![
        "https://www.baidu.com".to_string(),
        "https://www.baidu.com".to_string(),
        "https://www.baidu.com".to_string(),
        "https://www.baidu.com".to_string(),
        "https://www.baidu.com".to_string(),
        "https://www.baidu.com".to_string(),
        "https://www.littley.top".to_string(),
        "https://www.littley.top".to_string(),
        "https://www.littley.top".to_string(),
        "https://www.littley.top".to_string(),
        "https://www.littley.top".to_string(),
        "https://www.littley.top".to_string(),
        "https://www.littley.top".to_string(),
        "https://blog.csdn.net/".to_string(),
        "https://blog.csdn.net/".to_string(),
        "https://blog.csdn.net/".to_string(),
        "https://blog.csdn.net/".to_string(),
        "https://blog.csdn.net/".to_string(),
        "https://blog.csdn.net/".to_string(),
    ];
    let fetches = futures::stream::iter(
        paths.into_iter().map(|path| {
            async move {
                match reqwest::get(&path).await {
                    Ok(resp) => {
                        match resp.text().await {
                            Ok(text) => {
                                println!("RESPONSE: {} bytes from {}", text.len(), path);
                            }
                            Err(_) => println!("ERROR reading {}", path),
                        }
                    }
                    Err(_) => println!("ERROR downloading {}", path),
                }
            }
        })
    ).buffer_unordered(8).collect::<Vec<()>>();
    println!("Waiting...");
    fetches.await;
}

Cargo.toml

[dependencies]
reqwest = { version = "0.10.1", features = ["json"] }
tokio = { version = "0.2.11", features = ["full"] }
futures = "0.3.4"

其他例子

use futures::{stream, StreamExt}; // 0.3.1
use reqwest::Client; // 0.10.0
use tokio; // 0.2.4, features = ["macros"]

const PARALLEL_REQUESTS: usize = 2; //控制並行請求數量,太大容易內存不足

#[tokio::main]
async fn main() {
    let client = Client::new();

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

    let bodies = stream::iter(urls)
        .map(|url| {
            let client = &client;
            async move {
                let resp = client.get(url).send().await?;
                resp.bytes().await
            }
        })
        .buffer_unordered(PARALLEL_REQUESTS);

    bodies
        .for_each(|b| {
            async {
                match b {
                    Ok(b) => println!("Got {} bytes", b.len()),
                    Err(e) => eprintln!("Got an error: {}", e),
                }
            }
        })
        .await;
}

另一個例子不控制並行數量

use futures::future; // 0.3.4
use reqwest::Client; // 0.10.1
use tokio; // 0.2.11

#[tokio::main]
async fn main() {
    let client = Client::new();

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

    let bodies = future::join_all(urls.into_iter().map(|url| {
        let client = &client;
        async move {
            let resp = client.get(url).send().await?;
            resp.bytes().await
        }
    }))
    .await;

    for b in bodies {
        match b {
            Ok(b) => println!("Got {} bytes", b.len()),
            Err(e) => eprintln!("Got an error: {}", e),
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章