A Visual Journey Through Async Rust

github.com

148 points by PaulHoule 2 days ago


OskarS - 2 days ago

Very neat visualizations! Really good way to demonstrate the underlying principles. Great article.

I will add one thing: the author makes much of how problematic CPU-intensive tasks are. That's true, but also: if you need to do a CPU-intensive operation, you need to tie up a CPU to do it, there's no getting around it. What you might want to do is break the computation up in to smaller chunks with `.await`s sprinkled in there or dispatch it to a background thread, but at some point you have to pay the cost in CPU time.

What is much more problematic is blocking I/O operations. That also ties up a CPU and blocks the event loop, but for no good reason. All the CPU is going to be doing is sleeping waiting for the network or hard-drives or whatever it is to do it's work, when it could be doing other useful things, processing other events. That kind of thing leads to huge under-utilization of your resources, and it kills your concurrency.

This means you really can't mix async I/O with blocking I/O: if you go async, ALL your I/O operations need to be async. Or you have to do it off the event loop in a background thread or something, but then you're losing all the benefits of using async in the first place.

inglor - 2 days ago

> Unlike Node.js, Rust's Tokio allows us to spawn a new Task and run futures within it.

Nice article! For future reference in Node.js this would be `worker_threads` or if you want a higher level tokio like API something like https://piscinajs.dev/

This has been possible in Node.js for quite a while

--

Also for blocking I/O CPU bound parallelism I've found Rayon to be a pleasure. It has a few caveats but it leverages the Rust race condition checking well and it's very ergonomic.

I happily mix it with `tokio` in an application that is CPU bound but has to perform some async I/O (due to the SDKs it uses)

vermilingua - 2 days ago

It’s never addressed in the article that the calculations running on spawned threads produce deformed sine curves. Is that just a result of how its plotted or do tokio threads have less accurate track of time?

binary132 - 2 days ago

This is not limited only to Rust and I'd be very curious to see a similar comparison across multiple runtimes!

jdnier - 2 days ago

> Alright! We managed to plot two sine waves in the most convoluted method ever!

Those sine waves start to resemble a multi-voice music score. You might get some interesting pitch variability depending on other processes that are running.

jwufasdfsds - 2 days ago

The async disaster can be avoided by using Plan9 or Inferno.

Don't misinterpret this as criticism of async-rust. It's required to work on legacy platforms like linux.

rc00 - 2 days ago

Shared 12 days ago: https://news.ycombinator.com/item?id=43675098

ModernMech - 2 days ago

"Visualizing futures runtime like this really makes some pennies drop."

What's it mean to drop pennies?