8 releases
0.3.1 | Nov 25, 2024 |
---|---|
0.3.0 |
|
0.2.1 | Nov 23, 2024 |
0.1.4 | Nov 20, 2024 |
#1731 in Network programming
919 downloads per month
31KB
395 lines
simple-concurrent-get
Make multiple concurrent HTTP GET requests with ease.
Usage
concurrent_get
#[tokio::main]
async fn main() {
let request_urls = vec![
"https://example.com/1",
"https://example.com/2",
"https://example.com/3",
"https://example.com/4",
// ...
];
// for example: maximum 3 concurrent request
let request_results = simple_concurrent_get::concurrent_get(request_urls, 3);
let _: Vec<_> = request_results
.await
.map(|result| match result {
Ok(response) => {
let url = response.url().to_owned();
let bytes = futures_executor::block_on(async{ response.bytes().await }).unwrap();
println!("Successfully got '{}' with '{}' bytes of content", url, bytes.len())
},
Err(e) => eprintln!("{}", e),
})
.collect();
}
concurrent_get_foreach
#[tokio::main]
async fn main() {
let request_urls = vec![
"https://example.com/1",
"https://example.com/2",
"https://example.com/3",
"https://example.com/4",
// ...
];
// for example: maximum 3 concurrent request
let _: Vec<_> = simple_concurrent_get::concurrent_get_foreach(request_urls, 3,
|result| match result {
Ok(response) => {
let url = response.url().to_owned();
let bytes = futures_executor::block_on(async{ response.bytes().await }).unwrap();
println!("Successfully got '{}' with '{}' bytes of content", url, bytes.len())
},
Err(e) => eprintln!("{}", e),
}
)
.await
.collect();
}
Dependencies
~4–15MB
~190K SLoC