Re:How to think about `async`/`await` in Rust
by dumdumchan on Wednesday July 05, 2023 @04:36AM
(#36597503)
Look at this program:
How would you do that using threads?
[ Parent
| on HN ]
Re:How to think about `async`/`await` in Rust
by anonymoushn on Wednesday July 05, 2023 @04:41AM
(#36597546)
This program has no concurrency so you could do it without threads.
Re:How to think about `async`/`await` in Rust
by csomar on Wednesday July 05, 2023 @04:45AM
(#36597575)
Not in Rust if you use something like Tokio.
Re:How to think about `async`/`await` in Rust
by anonymoushn on Wednesday July 05, 2023 @04:46AM
(#36597593)
I'm pretty sure this program has no concurrency even if you port it to Rust and use Tokio.
Re:How to think about `async`/`await` in Rust
by csomar on Wednesday July 05, 2023 @07:45AM
(#36598971)
This particular one, yes. However, most programs are a bit more complicated than that. That and Async is mostly used for io operations. In which case, the value here is not from non-blocking operations but from freeing up the CPU while this operation finishes execution. You can think of that as another form of executing this program on a separate thread (since the CPU will be freed to do other tasks).
Re:How to think about `async`/`await` in Rust
by anonymoushn on Wednesday July 05, 2023 @07:52AM
(#36599049)
Doing a blocking read also frees your CPU to do other tasks!
Re:How to think about `async`/`await` in Rust
by yuvadam on Wednesday July 05, 2023 @04:44AM
(#36597572)
uh, open a thread that does that and `join` on it waiting for it to complete, but why would you need that in your example?
Re:How to think about `async`/`await` in Rust
by shellac on Wednesday July 05, 2023 @06:23AM
(#36598313)
Which, ironically, is what ought to have been done in the example using join!(a,b).await.
Re:How to think about `async`/`await` in Rust
by flohofwoe on Wednesday July 05, 2023 @04:47AM
(#36597604)
For this specific case, just use traditional blocking functions:
val another_path = readFileSync(path);
val data = readFileSync(another_path);
console.log(data.length);
The "runtime" (for instance the OS, or a task system) will take care of scheduling other things that are ready to to run while the the blocking functions are "stuck". That's exactly what processes and threads had been invented for.
Re:How to think about `async`/`await` in Rust
by usrbinbash on Wednesday July 05, 2023 @07:03AM
(#36598611)
Exactly like that, but using blocking functions. Then I run the code in a worker pool or in a greenlet runtime. The kernel and runtime take care of putting threads to sleep when they hit blocking IO.