There is an infinite loop { } declared here, why does it not cause CPU (core) to be blocked at 100% like when using while (true) { }? There is no use of sleep() or equivalents either which could "free" up the CPU.
use async_std::io;
use async_std::task;
async fn async_read_stdin() {
loop {
let mut input = String::new();
let text = io::stdin().read_line(&mut input).await.unwrap();
println!("You entered: {}", text);
}
}
fn main() {
task::block_on(async_read_stdin());
/// Some other code
}
Also, how can I get the program to continue with /// some other code while the program waits on async_read_stdin? As in when the some thing arrives on stdin, the context switches to execute that input otherwise will continue with /// some other code. Threads is one way I can think of.
Your code goes back and forth though the whole asynchronous machinery, but it actually behaves 100% like sync code, because:
you .await on every line, so lines are read one by one between print statements, exactly like in sync code. You have no other work to switch to to do in .await while it waits for I/O.
you run only one instance of this code, blocking main thread on it.
So despite using async libraries, the code isn't behaving like async code at all.
if you remove block_on, or block on join of multiple futures, you will be able to run more things at once and take advantage of some parallelism.
just reading and printing lines is probably too simple task to really need clever machinery. If you did something more complex with the lines, it could make sense to read on one thread, and send lines over mpsc channel to another thread to process it. That actually works fine with regular non-async reading and queues, or rayon.
You need to call join on the returned JoinHandle to prevent main from completing, just like with threads. You can do this with task::block_on because JoinHandle is a Future
Just to follow-up: Though bit dated (2016) and I am not sure how far has the eventual implementation in 1.39 has aligned with it, this blog post along with the chapter two of the async book really helped in developing some understanding about Rust's async model.