[Python-Dev] Importance of "async" keyword
ron3200 at gmail.com
Fri Jun 26 20:06:04 CEST 2015
On 06/26/2015 10:31 AM, Chris Angelico wrote:
> Apologies if this is a really REALLY dumb question, but... How hard
> would it be to then dispense with the await keyword, and simply
> _always_ behave that way? Something like:
> def data_from_socket():
> # Other tasks may run while we wait for data
> # The socket.read() function has yield points in it
> data = socket.read(1024, 1)
> return transmogrify(data)
> def respond_to_socket():
> while True:
> data = data_from_socket()
> # We can pretend that socket writes happen instantly,
> # but if ever we can't write, it'll let other tasks wait while
> # we're blocked on it.
> socket.write("Got it, next please!")
> Do these functions really need to be aware that there are yield points
> in what they're calling?
I think "yield points" is a concept that needs to be spelled out a bit
clearer in the PEP 492.
It seems that those points are defined by other means outside of a function
defined with "async def". From the PEP...
* It is a SyntaxError to have yield or yield from expressions
in an async function.
So somewhere in an async function, it needs to "await something" with a
yield in it that isn't an async function.
This seems to be a bit counter intuitive to me. Or am I missing something?
More information about the Python-Dev