[Python-Dev] PEP 492: async/await in Python; version 5
njs at pobox.com
Tue May 5 22:14:50 CEST 2015
On May 5, 2015 12:40 PM, "Jim J. Jewett" <jimjjewett at gmail.com> wrote:
> On Tue May 5 18:29:44 CEST 2015, Yury Selivanov posted an updated PEP492.
> Where are the following over-simplifications wrong?
> [Note that the actual PEP uses iteration over the results of a new
> __await__ magic method, rather than .result on the object itself.
> I couldn't tell whether this was for explicit marking, or just for
> efficiency in avoiding future creation.]
> (4) "await EXPR" is just syntactic sugar for EXPR.result
> except that, by being syntax, it better marks locations where
> unrelated tasks might have a chance to change shared data.
> [And that, as currently planned, the result of an await isn't
> actually the result; it is an iterator of results.]
This is where you're missing a key idea. (And I agree that more high-level
docs are very much needed!) Remember that this is just regular single
threaded python code, so just writing EXPR.result cannot possibly cause the
current task to pause and another one to start running, and then magically
switch back somehow when the result does become available. Imagine trying
to implement a .result attribute that does that -- it's impossible.
Writing 'x = await socket1.read(1)' is actually equivalent to writing a
little loop like:
# figure out what we need to happen to make progress
needed = "data from socket 1"
# suspend this function,
# and send the main loop a message telling it what we need
reply = (yield needed)
# okay, the main loop woke us up again
# let's see if they've sent us back what we asked for
if reply.type == "data from socket 1":
# got it!
x = reply.payload
# if at first you don't succeed...
(Now stare at the formal definition of 'yield from' until you see how it
maps onto the above... And if you're wondering why we need a loop, think
about the case where instead of calling socket.read we're calling http.get
or something that requires multiple steps to complete.)
So there actually is semantically no iterator here -- the thing that looks
like an iterator is actually the chatter back and forth between the
lower-level code and the main loop that is orchestrating everything. Then
when that's done, it returns the single result.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-Dev