-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
futures::future::join_all on JoinHandle is too slow #2401
Comments
This is for handle in sub_tasks {
handle.await.unwrap();
} On my laptop this gives:
This is because every single time one of the futures given to If you need more control than the for loop I suggested above, the Example using FuturesUnordereduse std::time::Instant;
use tokio::stream::StreamExt; // for .next()
#[tokio::main]
async fn main() {
let spawn_time = Instant::now();
let mut sub_tasks = futures::stream::FuturesUnordered::new();
let (tx, mut rx) = tokio::sync::mpsc::channel(1000);
// Spawn & send
for _ in 0..1_000_000 {
let mut sender = tx.clone();
let handle = tokio::spawn(async move {
sender.send(0).await.unwrap();
});
sub_tasks.push(handle);
}
// Receive
for _ in 0..1_000_000 {
let _ = rx.recv().await;
}
println!(
"Task spawned and received message: {}ms",
spawn_time.elapsed().as_millis()
);
let join_all_time = Instant::now();
while let Some(item) = sub_tasks.next().await {
let () = item.unwrap();
}
println!(
"Joining all handles: {}ms",
join_all_time.elapsed().as_millis()
);
} Using
I'm closing this because the issue lies in the futures crate, and not in Tokio. You should however feel free to post additional questions below. |
Thank you Darksonn! This was a very helpful. |
See tokio-rs/tokio#2401 Signed-off-by: 20k-ultra <[email protected]>
See tokio-rs/tokio#2401 Signed-off-by: 20k-ultra <[email protected]>
See tokio-rs/tokio#2401 Signed-off-by: 20k-ultra <[email protected]>
See tokio-rs/tokio#2401 Signed-off-by: 20k-ultra <[email protected]>
See tokio-rs/tokio#2401 Signed-off-by: 20k-ultra <[email protected]>
Tokio version: 0.2.16
Hi everyone,
I'm trying to hunt down some performance issues in my app. I have built a completely un-scientific benchmark to demonstrate my issue here:
And I get the following results:
I just can't figure out why it's 33x faster to:
Instead of:
Am I missing something obvious here? Is there a better way to await all sub-tasks to finish? From my perspective it looks like I would be better of manually signalling when the tasks finishes at the end of the closure by sending a
TaskEnded
message back to the parent instead of usingfutures::future::join_all()
.The text was updated successfully, but these errors were encountered: