I have a simple code like this to simulate how asynchronous code works on a blocking operation.
I'm expecting all these "Hello" prints will be shown after a 1000ms.
But this code works like a normal blocking code, each hello_wait call waits 1000ms and prints another Hello after 1000ms.
How can I make it run concurrently?
use std::{time::Duration};
use async_std::task;
async fn hello_wait(){
task::sleep(Duration::from_millis(1000)).await;
println!("Hello");
}
#[async_std::main]
async fn main() {
hello_wait().await;
hello_wait().await;
hello_wait().await;
hello_wait().await;
hello_wait().await;
}
This is what is happening:
// -- Wait 1000ms --
Hello
// -- Wait 1000ms --
Hello
// -- Wait 1000ms --
Hello
// -- Wait 1000ms --
Hello
// -- Wait 1000ms --
Hello
This is what I want to:
// -- Wait 1000ms --
Hello
Hello
Hello
Hello
Hello