Replacing Infinite while Loops with an Iterator: async edition

I first learned the ``for chunk in iter(lambda: sock.recv(N), b'')`` trick from page 138 of Dave Beazley’s fantastic Python Cookbook (“4.16. Replacing Infinite while Loops with an Iterator”), and never looked back. When I started to play with consuming sockets asynchronously, it occurred to me that it would be nice if we could write an async version of this, as in ``async for chunk in aiter(...)``. Is anyone working on adding that to Python, or at least interested too? Thanks, Josh

jab@math.brown.edu wrote:
it would be nice if we could write an async version of this, as in ``async for chunk in aiter(...)``.
The time machine seems to have taken care of this: https://docs.python.org/3.6/reference/compound_stmts.html#the-async-for-stat... -- Greg

On Sat, Jun 23, 2018 at 5:58 PM, Greg Ewing <greg.ewing@canterbury.ac.nz> wrote:
He's asking for an async version of the 'iter' builtin, presumably something like: async def aiter(async_callable, sentinel): while True: value = await async_callable() if value == sentinel: break yield value -n -- Nathaniel J. Smith -- https://vorpus.org

jab@math.brown.edu wrote:
it would be nice if we could write an async version of this, as in ``async for chunk in aiter(...)``.
The time machine seems to have taken care of this: https://docs.python.org/3.6/reference/compound_stmts.html#the-async-for-stat... -- Greg

On Sat, Jun 23, 2018 at 5:58 PM, Greg Ewing <greg.ewing@canterbury.ac.nz> wrote:
He's asking for an async version of the 'iter' builtin, presumably something like: async def aiter(async_callable, sentinel): while True: value = await async_callable() if value == sentinel: break yield value -n -- Nathaniel J. Smith -- https://vorpus.org
participants (3)
-
Greg Ewing
-
jab@math.brown.edu
-
Nathaniel Smith