3 ms·
That’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
by Someone 6y ago
That’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.
- steveklabnik 6y agoI gave a pair of talks on the background and history here. They may help? Transcripts + video: * 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/ I'd be re-writing what's in there, so check those out and if it's still confusing, I'll check back in this thread (there's a joke here somewhere...) later.
- rustshellscript 6y agoThanks Steve. I have studied the "200 lines" book a few days ago, but was still confused. Only after seeing your talks, everything becomes so much clearer. These talks should really be part of the "async book" :)
- steveklabnik 6y agoThe code in the second one is explaining the code in the async book! So it sorta kinda is.
- millstone 6y ago> if a future is inert, how can it ever detect that it is ready to make progress and call wake This is the crux, and I wish the discussion started by posing and answering this question. Futures are NOT inert. This is referenced only obliquely: When a future is not ready yet, poll returns Poll::Pending and stores a clone of the Waker copied from the current Context. This Waker is then woken once the future can make progress The passive voice "the waker is woken" obscures the fact that the Future itself is responsible for arranging the call to wake(). This means that the Future maintains some sort of dual active presence: it owns a thread, or a timer, or adds a file descriptor to an fd set, etc. An example from futures::io::AsyncRead::poll: If no data is available for reading, the method returns Poll::Pending and arranges for the current task to receive a notification when the object becomes readable or is closed. So `poll()` is a misleading name: it is not a passive check but a hook for the Future to materialize its dual, active half.
- chrisseaton 6y ago> 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” The whole original point of futures was that there was no ticket and no checking - when you used the value it blocked. I think what's described here is more like what was originally called an eventual value. But most people seem to have forgotten these original ideas and definitions.