Skip to content

Instantly share code, notes, and snippets.

@albe-rosado
Last active October 11, 2023 03:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save albe-rosado/64b33d2bba0896efab370ef1e31a14fd to your computer and use it in GitHub Desktop.
Save albe-rosado/64b33d2bba0896efab370ef1e31a14fd to your computer and use it in GitHub Desktop.
use futures::StreamExt;
use std::{
thread::{self},
time::Duration,
};
async fn do_work(idx: u32) {
thread::sleep(Duration::from_millis(500));
dbg!(idx);
}
fn main() {
let runtime = tokio::runtime::Builder::new_multi_thread()
.worker_threads(5)
.thread_name("multithreaded-runtime")
.build()
.unwrap();
{
runtime.block_on(async {
futures::stream::iter((0..100).into_iter().map(|num| tokio::spawn(do_work(num))))
.buffer_unordered(10)
.collect::<Vec<_>>()
.await;
});
}
println!("Done!");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment