[Python-Dev] async/await in Python; v2 (original) (raw)

Andrew Svetlov andrew.svetlov at gmail.com
Thu Apr 23 15:44:15 CEST 2015


On Thu, Apr 23, 2015 at 3:27 PM, Wolfgang Langner <tds333+pydev at gmail.com> wrote:

On Thu, Apr 23, 2015 at 12:35 PM, Paul Sokolovsky <pmiscml at gmail.com> wrote:

Hello, On Thu, 23 Apr 2015 12🔞51 +0300 Andrew Svetlov <andrew.svetlov at gmail.com> wrote: [] > > 3. > > async with and async for > > Bead idea, we clutter the language even more and it is one more > > thing every newbie could do wrong. > > for x in y: > > result = await f() > > is enough, every 'async' framework lived without it over years. > > async for i in iterable: > pass > > is not equal for > > for fut in iterable: > i = yield from fut But people who used Twisted all their life don't know that! They just know that "async for" is not needed and bad. I don't think it is bad nor not needed, but the syntax is not beautiful and for the 90% not doing async stuff irritating and one more thing to learn and do right/wrong. I had also a need for async loop. But there are other solutions like channels, not needing a new syntax. By channels do you mean something like asyncio.Queue? It requires that producer and consumer should be separate tasks. Often it works (with some performance penalty cost) but creating 2 tasks is not always obvious way to solve problem.

Also possible a function returning futures and yield in the loop with a sentinel. A proposal looks like guess to avoid for loop and use while everywhere.

Just compare while loop:

it = iter(it) while True: try: i = next(it) process(i) except StopIteration: break

with for alternative:

for i in it: process(i)

All this goes the road down to a producer consumer pattern. Nothing more. I think one of the most convenient consumer-producer pattern implementation in Python is for loop and iterators concept. It's sometimes too limited but works pretty well in 95% of use cases.

-- Thanks, Andrew Svetlov



More information about the Python-Dev mailing list