Skip to content

Waitgroup in tokio! #5319

Answered by Darksonn
ab-basit asked this question in Ideas
Dec 27, 2022 · 4 comments · 1 reply
Discussion options

You must be logged in to vote

The standard use-case for WaitGroup in go is to wait for a set of tasks to finish, but the best way to do that in Tokio is to store a Vec<JoinHandle<T>> and use a loop:

let mut join_handles = Vec::new();
for i in 0..10 {
    join_handles.push(tokio::spawn(my_async_fn(i)));
}
for jh in join_handles {
    jh.await.unwrap();
}

You can also use a JoinSet instead of the vector.

Replies: 4 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Answer selected by hawkw
Comment options

You must be logged in to vote
1 reply
@Noah-Kennedy
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Ideas
Labels
None yet
5 participants