![]() It is the reactor that will know the best time to poll that future again, and it will do so calling wake(). ![]() awaits, the reactor is on the Hades, working alongside the system I/O, doing the heavy lifting. While the executor is on the Olympus, managing things, listening to prayers. That allows the executor to know that the associated task is ready to move on.īut what is the reactor? It is the executor's brother. It will call the wake() function on the Waker that was passed as an argument in the poll() function. And who is going to tell him? The "reactor". await will return T and the executor will get rid of the future, so it does not get polled again.Īlternatively, if the polled future wasn't able to do all the work, it will return Pending.Īfter receiving Pending, the executor will not poll the future again until it is told so. await, the future was polled and returned Ready. await, who are going to do the work is an executor. "Run" here means delivering it to an "executor" that will call poll() in the future.īut what is the executor? Oversimplifying, it is a schedule algorithm that will actually poll the futures. I will not cover Pin here, as it is somewhat complex and not necessary to understand what is going on here.Īs hinted by the code above, futures in Rust are lazy, which means that just declaring them will not make them run. Suppose we have a Future created via async keyword. And to illustrate this, I came up with a simplified hypothetical scenario. Lots of words! But I honestly think it is easier to bundle everything together because it is easier to understand what they do in context. Poll, Context, Waker, Executor and Reactor ![]() However, we have some ground to cover before getting there. The question is, if it returns Pending, how do we get back at it, so it can keep working towards completion? The short answer is the reactor. The general idea behind this function is simple: when someone calls poll() on a future, if it went all the way through completion, it returns Ready(T) and the. This enum is the representation of what I wrote earlier, that a Future represents a value that may or may not be ready. In short, we use the async keyword to tell Rust that a block or a function is going to be asynchronous.Įnter fullscreen mode Exit fullscreen modeĪs you can see, my description of. You can combine concurrent and parallel programmin (e.g., by spawning futures), but I will not cover it here since async/.await is used to enable concurrent programming, so that is my focus here. Concurrent means that different tasks will perform their activities alternatively e.g., task A does a bit of work, hands the thread over to task B, who works a little and give it back, etc.ĭo not confuse it with parallel programming, where different tasks are running simultaneously. The async book states that async is a concurrent programming model. await) are the centerpieces of writing async Rust. It means that these two keywords ( async and. Otherwise, let's go :)Īsynchronous Rust (async Rust, for short) is delivered through the async/.await syntax. So, if you want something in a more intermediary level, go straight to the content listed above. With this amount of superb information, why writing about it? My answer here is the same for almost every other entry on my DEV blog: to reach an audience for which this content is still a bit too hard to grasp. Jon's stream on how Futures and async/await works.Without Boats' proposal for await syntax (the other entries with the tags async and Future are also excellent).Steve's talks on Rust's Journey to async/await and on how it works.The Asynchronous Programming in Rust, a.k.a.TL DR: I will try to give an easy-to-understand account of some concepts surrounding asynchronous Rust: async, await, Future, Poll, Context, Waker, Executor and Reactor.Īs with most things I write here, we already have good content related to asynchronous Rust.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |