I would like to run a sequence of streams with several processes of the stream running concurrently. The test code that I tried is the following:
use tokio;
use tokio_stream::{self as stream, StreamExt};
use std::time::Duration;
#[tokio::main]
async fn main() {
let stream = stream::iter(0..10)
.then(|i| async move {
tokio::time::sleep(Duration::from_secs(1)).await;
println!("i={:?}", i);
})
.chunks_timeout(3, Duration::from_secs(20));
let _result : Vec<_> = stream
.collect().await;
}
This code runs but it prints the 10 values one by one with a 1 second delay. This is the opposite of concurrency. What I would expect is that one wait 3 seconds, get 3 numbers printed, then wait 1 second and so on.
I think the tokio::time::sleep
is fine as with a join_all
I got the code working concurrently.
So what explains the lack of concurrency and what be done about it?