Last active
October 11, 2023 03:05
-
-
Save albe-rosado/64b33d2bba0896efab370ef1e31a14fd to your computer and use it in GitHub Desktop.
This file contains hidden or 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::{ | |
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