Trying to understand async in rust

I have tried to read up on the rust documentation and viewed steve klabnik's talks. But still trying to piece things together.

Am i right to say that

  1. Rust implementation of non blocking computation i.e. async is like Node.js: as an event loop?
  2. But that effectively makes it single threaded which is not very performant which is why the green thread concept is also present and introduced by runtimes like for example tokio
  3. So this is effectively a mix of both event loop and green threads in that there are multiple event loops which acts like green thread and scheduled by say tokio runtime???

Admittedly i do not know this topic very well and have read on topics like:

  • Blocking vs Non Blocking IO
  • CPU vs IO bound computation
  • Event loop vs Green threads

Rust takes a somewhat different approach to futures than Node: Rust the language (and its standard library) define what a Future is but not how it is run. Rust itself doesn't come with an event loop that would drive the execution of your futures.
What the language gives you is ways of constructing futures (like the async keyword which transforms functions into a future / state machine). It is up to the user to actually drive execution of these futures.

That's where libraries like tokio come into play which implement what would be comparable to an event loop in Node. You hand the futures you create to such a library and it will drive the execution of these futures. How such an Executor actually does this is up to the implementation. It could be single threaded, M:N threaded or any other model you could think of.

Take a look at this thread.

No, it's not like Node. There isn't one global built-in event loop.

It's not like Go. There are no green threads with magical treading/blocking on system calls. Proto-rust used to have this long long time ago before 1.0 and it was dropped.

Rust — the language — doesn't have any event loop at all. It can't even run anything async. All it knows is how to transform async fn code into a Future state machine that has a poll method that performs one step of computation at a time.

Then you add your own executors (tokio, async-std) which know how to call poll on many Future objects. They can do the polling single threaded like Node, or multi-threaded from a thread pool.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.