-
New to rust, new to tokio, new to async... Once I have more than 128 Futures in a FuturesUnordered struct though the program keeps running at 100% CPU and won't finish. Behavior looks similar to rust-lang/futures-rs#2140 though I´m not using join_all (tried that before).
#[tokio::main]
async fn main() {
block_on(as_main());
}
async fn as_main() {
let mut v= FuturesUnordered::new();
// Works fine like this:
// for i in 0 .. 128 {
// Will keep hanging at 100% CPU with >= 128 futures
for i in 0 .. 129 {
let future = rnd_delay(i);
v.push(future);
}
println!("Handles pushed...");
loop {
let res = v.next().await;
match res {
Some(_) => (),
None => {
println!("Done");
break
},
}
}
}
async fn rnd_delay(i : u32) {
let mut rng = rand::thread_rng();
let millis: u64 = rng.gen_range(99..101);
tokio::time::sleep(Duration::from_millis(millis)).await;
let mut msg:String = String::from("Message from: ");
msg.push_str(&i.to_string());
println!("{}" , msg);
} tokio version 1.17.0 |
Beta Was this translation helpful? Give feedback.
Answered by
taiki-e
Feb 17, 2022
Replies: 1 comment 2 replies
-
Perhaps this block_on is causing the problem async fn main() {
- block_on(as_main());
+ as_main().await;
} |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
emogames
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Perhaps this block_on is causing the problem