Created
August 14, 2020 00:05
-
-
Save LeonAlvarez/a19f8f04a4d4207988b2e32d4871b336 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use futures::{StreamExt}; | |
use std::time::Duration; | |
use async_std::task; | |
#[tokio::main] | |
async fn main() -> Result<(), Box<dyn std::error::Error + 'static>> { | |
let data:Vec<u32> = (0..50).collect(); | |
println!("Waiting..."); | |
let fetches = futures::stream::iter( | |
data.into_iter().map(|id| { | |
let x = id.clone(); | |
async move { | |
let request = async { x }; | |
task::sleep(Duration::from_secs(1)).await; | |
request.await; | |
} | |
}) | |
).buffer_unordered(12).collect::<Vec<()>>(); | |
fetches.await; | |
println!("Done..."); | |
/* | |
stream::iter(data).for_each(|id| async move { | |
let request = async { id }; // async io request | |
let res = request.await; | |
println!("res: {:?}", res); | |
() | |
}).await; | |
*/ | |
Ok(()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment