-
Can we have WaitGroup like feature in tokio? |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 1 reply
-
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 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 |
Beta Was this translation helpful? Give feedback.
-
@Darksonn understood! but there could be complex workflow where we want to make sure all tasks are completed. |
Beta Was this translation helpful? Give feedback.
-
There is a library called awaitgroup that might be useful: https://crates.io/crates/awaitgroup I am not the author, but I have used this in the past. Has an interface similar to |
Beta Was this translation helpful? Give feedback.
-
You can also try https://github.com/laizy/waitgroup-rs I made 3 years ago. It is tiny and performant :
|
Beta Was this translation helpful? Give feedback.
-
A newer solution: |
Beta Was this translation helpful? Give feedback.
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:You can also use a
JoinSet
instead of the vector.