8 ms·
Futures explained in 200 lines of Rust
- johnnylambada 6y agoWhen I read the title I thought it was going to be about financial futures.
- 0x63_Problems 6y agohttps://web.archive.org/web/20200511234503/https://stjepang.github.io https://web.archive.org/web/20200511234503/https://stjepang.... The first blog mentioned in the article appears to be down now, last snapshot I found is this one.
- gwbas1c 6y agoAs someone who's learning async rust, I'm really looking forward to reading through this in the next day or so. I'm especially interested in learning how to implement an executor.
- brobinson 6y agoI recently spent 15 minutes going over a Rust futures tutorial and was then able to get a Rust program streaming live bitcoin futures (coincidence!) over websocket connections from multiple exchanges working in less than an hour. I then then attempted to port some Python code to use Python's new async/await and was unsuccessful. I managed to get a bunch of async workers running, but only the first one would process jobs from the queue or the entire program would just hang indefinitely. Couldn't figure it out after two hours and just gave up on it. Nice work, Rust team. This is a game changer for me.
- ygram 6y agoPython got async/await in 2015, so not exactly new.
- brobinson 6y agoI still find people writing new code in Python 2 so I consider 3.7.x to be "new", but you are right.
- deleted 6y ago[deleted]
- jib_flow 6y agoAs someone who has struggled learning Rust, it's incredible to me you were able to do this so quickly. Had you spent much time prior with Rust?
- brobinson 6y agoI've been using it off and on since late 2014, back when my code would stop working every week due to a new breaking change. :D
- martamorena9182 6y agoLooking at the code, it's a nightmare to parse. There are so many details to deal with that should never bother you under normal circumstances (the core problem of using Rust where it's not necessary). It's admirable that you have such an easy time with it. However, for most people, using Rust for these purposes is absolutely not recommended. Java/Kotlin paired with reactive streams makes these tasks super easy to read, once you get the hang of reactive streams. Also there are complexities in there, you just simply can't brush all the intricacies of network programming and multi-threading under the rug, but at least you are dealing only with the complexities that are necessary for your problem. With Rust you are dealing with tons of additional complexity that doesn't matter to 99% of people & problems. I love that Rust exists and it has its uses (browsers, hypervisors, drivers, kernels, etc). I just find it very alienating when people compare this to Python and do everyday tasks with it. No, you really don't want to deal with borrowing, memory management and life-times, unless it is part of the problem you are trying to solve (and there aren't too many for which that applies).
- steveklabnik 6y agoThis code is all about learning about how this stuff works at a deeper level. To use async Rust, you need to write none of this code.
- martamorena9182 6y agoYes, this is usually how it turns out in practice. You don't need to understand what's going on under the hood, you just string some libraries together and call it a day ;). /sarcasm off
- steveklabnik 6y agoAbstractions and use of them is the core of our discipline, not sure why you think your comment is sarcastic.
- db48x 6y agoThis article is explaining how Futures work by showing you what's under the covers. You don't actually have to write this code yourself, you can just use one of many wonderful libraries that handle it all for you. As it says in the second paragraph: "Going into the level of detail I do in this book is not needed to use futures or async/await in Rust. It's for the curious out there that want to know how it all works."
- Someone 6y agoThat’s more “Futures in Rust explained in 200 lines (of Rust)”. Explaining futures doesn’t require 200 lines: when a function doesn’t return what you asked for, but a ticket that you can later use to check whether what you asked for is available or exchange for what you asked for, that ticket is called “a future”. Functions returning futures allow you to do some other work while the function does what you asked it to do. I think most won’t need more than that to make sense of https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Future.html https://docs.oracle.com/javase/7/docs/api/java/util/concurre... or http://www.cplusplus.com/reference/future/future/ http://www.cplusplus.com/reference/future/future/. However, reading this article _and_ https://doc.rust-lang.org/std/future/trait.Future.html https://doc.rust-lang.org/std/future/trait.Future.html, I don’t understand why Rust decided to provide a more difficult API (“runtime-implementors can come up with interesting new wake-up mechanisms”, according to https://cfsamson.github.io/books-futures-explained/2_waker_context.html https://cfsamson.github.io/books-futures-explained/2_waker_c..., but an example would help there. I can’t come up with one that can’t be done with the simpler interface) So, what am I missing here? Is this actually used in libraries, or is it something that might end up being YAGNI?
- bombela 6y agoThat's the part that is difficult: Efficient wake up on completion. Network I/O is usually monitored via a so called I/O loop. Local file I/O are often not exposed with asynchronous APIs (yes I know it's changing on linux quickly). But what about locks? Let's say you want a tread safe queue, how do you wake up the right function when the mutex is unlocked? I suspect this is the reason of all this complexity.
- Someone 6y agoI still don’t understand. If you want wake-up on completion, you create a thread/green thread/task/coroutine (whatever fits your bill) and have it await the future (likely also make the call that creates it) What do you get by doing that inside the future machinery? Maybe fewer tasks and their call stacks, but is that a huge problem? Also, what’s with wake and poll? https://doc.rust-lang.org/std/future/trait.Future.html https://doc.rust-lang.org/std/future/trait.Future.html: “Futures alone are inert; they must be actively polled to make progress, meaning that each time the current task is woken up, it should actively re-poll pending futures that it still has an interest in. The poll function is not called repeatedly in a tight loop -- instead, it should only be called when the future indicates that it is ready to make progress (by calling wake())” So, an asynchronous task that doesn’t have an answer yet calls wake to tell some object that it has to call poll on the task, so that the asynchronous task can make more progress? Why doesn’t it just do what it was asked to do until it has an answer? Also, if a future is inert, how can it ever detect that it is ready to make progress and call wake? I really do not understand this design. I guess a UML sequence diagram (https://en.mwikipedia.org/wiki/Sequence_diagram https://en.mwikipedia.org/wiki/Sequence_diagram) would help me. It would show what entities exist (if Rust’s future is inert, it can’t be much more than the memory block holding the function result and a reference to the async code. If so, what’s doing the computation?) put names on them, and show what calls they make.