Cannot step through asynchronous iterator manually

Oscar Benjamin oscar.j.benjamin at gmail.com
Sat Jan 30 13:22:21 EST 2016


On 30 January 2016 at 16:42, Ian Kelly <ian.g.kelly at gmail.com> wrote:
>> AFAICT there's no generator-function-style syntax for writing an async
>> iterator so you'd have to make a class with the appropriate methods if
>> you wanted to be able to loop over aslice with async for.
>
> Before you go any further with this, be sure to check out the aitertools
> third-party module. I haven't done anything with it myself, but it already
> claims to provide aiter and anext as well as async versions of everything
> in the standard itertools module.

Right you are. There is aslice and it is implemented as a class with
__anext__ etc. methods:
https://github.com/asyncdef/aitertools/blob/master/aitertools/__init__.py#L747

My original suggestion just becomes:

from aitertools import alist, islice

rows = await alist(islice(cur, 2))  # pull at most 2 rows

--
Oscar



More information about the Python-list mailing list