[Python-Dev] async/await in Python; v2

Yury Selivanov yselivanov.ml at gmail.com
Sat Apr 25 23:02:06 CEST 2015


Hi Arnaud,

On 2015-04-25 4:47 PM, Arnaud Delobelle wrote:
> On Tue, 21 Apr 2015 at 18:27 Yury Selivanov <yselivanov.ml at gmail.com> wrote:
>> Hi python-dev,
>>
>> I'm moving the discussion from python-ideas to here.
>>
>> The updated version of the PEP should be available shortly
>> at https://www.python.org/dev/peps/pep-0492
>> and is also pasted in this email.
>>
> Hi Yury,
>
> Having read this very interesting PEP I would like to make two
> remarks.  I apologise in advance if they are points which have already
> been discussed.
>
> 1.  About the 'async for' construct.  Each iteration will create a new
> coroutine object (the one returned by Cursor.__anext__()) and it seems
> to me that it can be wasteful.  In the example given of an 'aiterable'
> Cursor class, probably a large number of rows will fill the cursor
> buffer in one call of cursor._prefetch().  However each row that is
> iterated over will involve the creation execution of a new coroutine
> object.  It seems to me that what is desirable in that case is that
> all the buffered rows will be iterated over as in a plain for loop.

I agree that creating a new coroutine object is a little bit
wasteful.

However, the proposed iteration protocol was designed to:

1. Resemble already existing __iter__/__next__/StopIteration
protocol;

2. Pave the road to introduce coroutine-generators in the
future.

We could, in theory, design the protocol to make __anext__
awaitable return a regular iterators (and raise
StopAsyncIteration at the end) to make things more
efficient, but that would complicate the protocol
tremendously, and make it very hard to program and debug.

My opinion is that this has to be addressed in 3.6 with
coroutine-generators if there is enough interest from
Python users.

>
> 2.  I think the semantics of the new coroutine objects could be
> defined more clearly in the PEP.  Of course they are pretty obvious
> when you know that the coroutines are meant to replace
> asyncio.coroutine as described in [1].  I understand that this PEP is
> mostly for the benefit of asyncio, hence mainly of interest of people
> who know it.  However I think it would be good for it to be more
> self-contained.  I have often read a PEP as an introduction to a new
> feature of Python.  I feel that if I was not familiar with yield from
> and asyncio I would not be able to understand this PEP, even though
> potentially one could use the new constructs without knowing anything
> about them.
>
>


I agree. I plan to update the PEP with some new
semantics (prohibit passing coroutine-objects to iter(),
tuple() and other builtins, as well as using them in
'for .. in coro()' loops).  I'll add a section with
a more detailed explanation of coroutine-objects.

Best,
Yury


More information about the Python-Dev mailing list