Status of lightweight processes in Rust

Hi Rust community!

As a first post, I would like to ask what the status is in July 2020 regarding lightweight processes in Rust.

  • Is there a support for lightweight processes (goroutine / channel) -like constructs as in Go?
  • Alternatively are there some mainstream crates which implement something similar?

Thanks!

Certainly. Just now I have started to use the tokio crate to create all kind of asynchronous processes, often tied together with channels.

See: https://docs.rs/tokio/0.2.21/tokio/index.html

You might be interested to check out how all this async "light weight threads" works in Rust: "The Talk You've Been Await-ing for" https://www.youtube.com/watch?v=NNwK5ZPAJCk

1 Like

That is the purpose of async/await and Tokio. We actually released a new tutorial for Tokio today, which you can find on our website.

3 Likes

Thank you very much !

Thank you very much!

Something to keep in mind is Rust's implementation is quite different to Go's, due to the differences in priority.

Goroutines each get their own stack and the runtime manages switching between stacks. In this way it's a similar model to normal OS threads and you don't even notice that there's any smart multitasking going on.

Rust's futures are more akin to syntactic sugar around the state machine you might write by hand when doing asynchronous programming. All the state you need is bundled up into a single object which can be passed around like any other value (although you may not necessarily be able to inspect its contents due to encapsulation and such).

This isn't as simple as the goroutine model, but has the benefit that almost everything can be implemented as libraries (that's actually how futures first started and were developed!), and because the structure of these nested state machines is known at compile time it gives the optimiser a better chance of generating fast code. There's also no need to switch stacks or have a pervasive runtime or GC because making progress on a future is just a case of calling its poll() method, something typically done by the Executor exposed in libraries like tokio or async-std so they can do fancy things like multitasking and only waking a future when it needs to make progress.

1 Like

On a quick scan through that is a gorgeous tutorial. Wish I had it a week ago!

2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.