[Python-Dev] Importance of "async" keyword

Paul Sokolovsky pmiscml at gmail.com
Fri Jun 26 16:51:13 CEST 2015


Hello,

On Sat, 27 Jun 2015 00:31:01 +1000
Chris Angelico <rosuav at gmail.com> wrote:

> On Sat, Jun 27, 2015 at 12:20 AM, Ethan Furman <ethan at stoneleaf.us>
> wrote:
> > As Nick said earlier: the caller always blocks; by extension (to my
> > mind, at least) putting an `await` in front of something is saying,
> > "it's okay if other tasks run while I'm blocking on this call."
> 
> 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 come from a background of thinking with threads, so I'm accustomed
> to doing blocking I/O and assuming/expecting that other threads will
> carry on while we wait. If asynchronous I/O can be made that
> convenient, it'd be awesome.

Some say "convenient", others say "dangerous".

Consider:

buf = bytearray(MULTIMEGABYTE)

In one function you do:

socket.write(buf),

in another function (coroutine), you keep mutating buf.

So, currently in Python you know if you do:

socket.write(buf)

Then you know it will finish without interruptions for entire buffer.
And if you write:

await socket.write(buf)

then you know there may be interruption points inside socket.write(),
in particular something else may mutate it while it's being written.
With threads, you always have to assume this last scenario, and do
extra effort to protect against it (mutexes and stuff).


Note that it's original thinking of Guido, but seeing how it all
combines in asyncio (far from being nice consistent way), it's not
surprising that some people will keep not understanding how asyncio
works/how to use it, while other will grow disappointed by it. Here's
my latest disappointment wrt to asyncio usage in MicroPython:
http://bugs.python.org/issue24449


-- 
Best regards,
 Paul                          mailto:pmiscml at gmail.com


More information about the Python-Dev mailing list