type
status
slug
date
summary
tags
category
password
icon
A mental model of how Futures works
The main goal in this part is to build a high level mental model of how the different pieces we read about in the previous chapter works together. I hope this will make it easier to understand the high level concepts before we take a deep dive into topics like trait objects and generators in the next few chapters.
This is not the only way to create a model of an async system since we're making assumptions on runtime specifics that can vary a great deal. It's the way I found it easiest to build upon and it's relevant for understanding a lot of real implementations you'll find in the async ecosystem.
Finally, please note that the code itself is "pseudo-rust"(rust的伪代码) due to the need for brevity and clarity.
注意这里一个await就产生了三个不同状态,类比分界线,画一条线分隔左右两边,再把中间的线本身(很粗)算上,这样就产生三个区域,状态也是这样,在加入一个await,会同样再增加两个状态
这里的WAKER解释可以抄出来:
A Waker is passed into Future::poll. It will hang on to that Waker until it reaches an `await` point. When it does it will call `poll` on that future and pass the Waker along.
We don’t actually pass a `Waker` directly. We pass the `Waker` as a part of an object called `Context` which might add extra context to the `poll` method in the future.
这里是走到leaf-future的创建,TIP强调一下:
Leaf futures are created by the Reactor. It represents an I/O resource, like a network call. Nothing special happens when we create the future…
这一步就特别关键了,是leaf_future执行它的poll方法,并且进行await操作
WAKER:
The Waker passed in to
non_leaf_fut
is passed on to the Reactor through the poll method of leaf_fut
The Waker is specific for the Executor, but the reactor only knows how to call <<wake>> on it. You can think of it as a trait object.
这里的CONCURRENCY补充提示很重要:
“non_leaf_fut” is the only one we put in the WAITTING queue even if we’re actually waiting on “leaf_fut”.
If we want two futures to run concurrently, we need to create another top-level future. A top-level future is often called a “task”.
Most Executors provide a method to “spawn” a new top-level future.
When you “spawn” a future you create a top-level future and place it in the “READY” queue to be scheduled at the next await point.
这里有个问题:好像future(或者说task)不主动的await,按这个道理,是不是就不会让READY里面的future(或者说task)得到执行,除非这个future执行完成,结束了。