How to simplify tokio::join!
when calls too much ?
#4442
-
tokio::join!(request("1".to_string()),
request("2".to_string()),
request("3".to_string()),
request("4".to_string()),
// If there are thousands of such calls, how to simplify it?
request("5".to_string())); Can calls like the following be supported? // here, not just Vec, better if LinkedList and other collection can be supported
let mut array = Vec::new();
array.push(request("1".to_string()));
tokio::join!(array); |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
When you are spawning a very large number of tasks, then you should be spawning them as separate tasks rather than using things like let mut handles = Vec::new();
for i in 0..1000 {
handles.push(tokio::spawn(request(i.to_string())));
}
let mut output = Vec::with_capacity(handles.len());
for handle in handles {
output.push(handle.await.unwrap());
} Be aware that the above code will run them in parallel even though they are not immediately We are currently working on new features to make this more convenient, see e.g. #4335. |
Beta Was this translation helpful? Give feedback.
When you are spawning a very large number of tasks, then you should be spawning them as separate tasks rather than using things like
tokio::join!
as this will lead to better performance. The code below shows how you can do that:Be aware that the above code will run them in parallel even though they are not immediately
.await
ed. This is due to thetokio::spawn
call, which will start any task passed to it immediately.We are currently working on new features to make t…