cashmere

cashmere

Reqwest Multithreading

use reqwest;
use tokio;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create an instance of the async client
    let client = reqwest::Client::new();

    // A list of URLs to scrape
    let urls = vec![
        "http://example.com",
        "http://example.org",
        "http://example.net",
    ];

    // Collect a list of futures
    let mut tasks = vec![];
    for url in urls {
        let client = &client;
        let task = tokio::spawn(async move {
            let res = client.get(url).send().await;
            match res {
                Ok(response) => {
                    println!("Status for {}: {}", url, response.status());
                    // Process response...
                }
                Err(e) => eprintln!("Error requesting {}: {}", url, e),
            }
        });
        tasks.push(task);
    }

    // Await the completion of all the tasks
    for task in tasks {
        task.await?;
    }

    Ok(())
}