11 ms·
Can someone explain to a non-rustacean what Tokio introduces that's not part of Rust? It looks like Rust provides the async/await semantics, so I'm guessing thi
by pfraze 6y ago
Can someone explain to a non-rustacean what Tokio introduces that's not part of Rust? It looks like Rust provides the async/await semantics, so I'm guessing this is an event loop and dispatching system?
- pfraze 6y agoReplying to myself, I found this bit in the docs explaining how Tokio decorates the main: > An async fn is used as we want to enter an asynchronous context. However, asynchronous functions must be executed by a runtime. The runtime contains the asynchronous task scheduler, provides evented I/O, timers, etc.
- steveklabnik 6y agoTo provide some more color on why this isn't built in, different runtimes provide different kinds of guarantees and performance profiles. A webapp has very different requirements than an embedded system, and so we don't want to provide a single runtime. The language contains the basic things needed for the ecosystem to exist, and interoperation points, and then leaves the rest to said ecosystem. (Some of those interoperation points are still being worked out, so it's not perfect yet.)
- pfraze 6y agoAs always, very elegant. Thanks for the added info.
- steveklabnik 6y agoYou're welcome, thanks :) If you want to dig deeper: * https://www.infoq.com/presentations/rust-2019/ https://www.infoq.com/presentations/rust-2019/ * https://www.infoq.com/presentations/rust-async-await/ https://www.infoq.com/presentations/rust-async-await/
- olalonde 6y agoWhen learning Rust a few months ago, I built a small client library for a REST API using reqwest (which uses Tokio). I then started writing a web app using Tide (https://github.com/http-rs/tide https://github.com/http-rs/tide). I eventually realized that it would be difficult to use the library I had built earlier since Tide uses the async-std runtime rather than Tokio. That was very disappointing. Is there any plan to make it easier to write "runtime agnostic" libraries in the future?
- steveklabnik 6y agoYes, that is what I alluded to at the end. There's a few points here that still need some interop work. The intention is to fix that, but it's non-trivial. We'll get there.
- bogeholm 6y agoSteve, just want to say thanks for your dedication - I find you’re always around when there’s a Rust thread on HN
- steveklabnik 6y agoYou’re welcome!
- mesh 6y agoI had the exact same experience. I think it can be a pretty big barrier to getting started, as you really have to lock into a sub-set of the ecosystem (i.e. I can only use crates that have support for Tokio). I understand the reasoning for not wanting this in the core language, but perhaps there could be some standard implementations which would still allow for custom runtimes.
- jkelleyrtp 6y agoYou can make async-std mimic a tokio runtime by adding “tokio02” or “tokio03” to the list of features for async_std. At its core, futures and future combinators work under all runtimes, it’s just some features will complain if they don’t detect the tokio runtime.
- 1f60c 6y agoI knew you needed to use a library like Tokio with asynchronous Rust, but I never understood why, so thanks. That said, the last time I looked at it, Tokio was much too complex for my tastes.
- aliceryhl 6y agoThe Rust language provides the async/await syntax, which can turn imperative code into Future objects, but to run those Future objects, you must repeatedly call their poll method. Tokio is the piece of code that calls that method. It does so in a manner such that futures are only polled if able to continue work, and not e.g. waiting for a timer. Besides that it provides lots of utilities for working with async code.
- programmarchy 6y agoWould it be fair to compare this to Apple's Grand Central Dispatch a.k.a. libdispatch?
- MuffinFlavored 6y agoTo me it's kind of like the event loop of node.js (which I believe comes from libuv)?
- lucacasonato 6y agoYes, Deno uses it as its internal event loop for JS code.
- steveklabnik 6y agoFun party fact: long ago, Rust had libuv built in too.
- shepmaster 6y ago... for not-so-fun parties. ;-)
- p5a0u9l 6y agowhy’d they move past it? does libuv not fit well in rust?
- shepmaster 6y ago> rustafarian The community prefers the term "rustacean" to be as inclusive as possible. Please keep that in mind in the future.
- pfraze 6y agoTIL, thanks for the correction
- steveklabnik 6y ago(It's a combination of not wanting to trivialize a religion, and that 'RESTafarian' has a long history of use, and that's just confusing)
- 02020202 6y agoyou sound like a proper cunt
- bumblebritches5 6y agolmao
- gabereiser 6y agotokio is to rust what asyncio is to Python.
- p5a0u9l 6y agoexcept say asyncio is pet of standard python
- gabereiser 6y agoand tokio is pet of rust... I don’t see the value of this argument. Tokio is known as being THE async executor in rust. Asyncio being THE executor in Python. In other languages there’s a number of frameworks to choose from. In this comparison there really is only one.
- remram 6y agoAnd nodejs or web eventloop is to JavaScript. They provide the executor and the timeout/IO primitives.
- pizza234 6y agoAFAIK, the major component is the scheduler. Rust can't really implement green threading (imagine Golang) by default, because it requires the runtime to be bundled in the executable, and the code to be compiled in a specific way to be managed by the scheduler. I actually find amusing the thought that _this_ is systems programming. In a low level language one can implement the functionality of a higher level language (ie. Golang), but not the reverse :-)
- mratsim 6y agoBoth Rust and Go used to have green threads i.e. stackful coroutines / fibers that installed their own execution stack and switched to it. However everyone who ever did green threads ended up having a lot of difficulties with stack optimization: - Go: https://docs.google.com/document/d/1wAaf1rYoM4S4gtnPh0zOlGzWtrZFQ5suE8qr2sD8uWQ/pub https://docs.google.com/document/d/1wAaf1rYoM4S4gtnPh0zOlGzW... - Rust: https://mail.mozilla.org/pipermail/rust-dev/2013-November/006314.html https://mail.mozilla.org/pipermail/rust-dev/2013-November/00... - Same for Java in the past. Now only Go is left. More on fibers woes from the C++ point of view: http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2018/p1364r0.pdf http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2018/p136...
- lmkg 6y agoIn very loose terms: Rust's async/await syntax defines an interface to async programming, not an implementation. Rust leaves the implementation, and thereby choice of concurrency strategy, to libraries. Tokio is such a library. There's at least one other popular one of note.
- ibraheemdev 6y ago> There's at least one other popular one of note. The other popular runtimes are async-std [0] and smol [1]. [0]: https://github.com/async-rs/async-std https://github.com/async-rs/async-std [1]: https://github.com/smol-rs/smol https://github.com/smol-rs/smol
- n42 6y agoif I understand correctly, smol was created by the async-std folks, and async-std now uses it as its runtime https://github.com/async-rs/async-std/pull/757 https://github.com/async-rs/async-std/pull/757
- indepnd 6y agoFrom what I've read, smol was created by its author and later on adopted by async-std. And, its author was in Tokio team, and later left and was in async-std team before he left and created smol. So smol was based on quite deep knowledge and experience in async.
- nx7487 6y agoYeah that's exactly right, it's an event loop. It's hilarious to me that it's hitting "1.0" now. I remember the original release ~4 years ago?? That was way before Rust even had async/await. I imagine there were quite a few refactors. I mean, so much work, to make a library for asynchronous network service programming? If I needed an event loop and a scheduler I think I would just invest in implementing it myself, tailored to the requirements of my project.
- octoberfranklin 6y agoTokio is basically the asynchronous standard I/O library for Rust. Async/await are language features. Tokio is the library for using these to do I/O. Rust has a really tiny standard library (by modern standards) and a very high standard for moving stuff into the standard library. Right now it has no "standard" asynchronous I/O library ("async-std" bequeathed that name on themselves; it doesn't ship with Rust).