Cannot step through asynchronous iterator manually

Maxime S maxischmeii at gmail.com
Sat Jan 30 07:05:17 EST 2016


2016-01-30 11:51 GMT+01:00 Frank Millman <frank at chagford.com>:

> "Chris Angelico"  wrote in message
> news:CAPTjJmoAmVNTCKq7QYaDRNQ67Gcg9TxSXYXCrY==S9Djjna_rA at mail.gmail.com...
>
>
>> On Sat, Jan 30, 2016 at 7:22 PM, Frank Millman <frank at chagford.com>
>> wrote:
>> > We had a recent discussion about the best way to do this, and ChrisA
>> > suggested the following, which I liked -
>> >
>> >    cur.execute('SELECT ...)
>> >    try:
>> >        row = next(cur)
>> >    except StopIteration:
>> >        # row does not exist
>> >    else:
>> >        try:
>> >            next_row = next(cur)
>> >        except StopIteration:
>> >            # row does exist
>> >        else:
>> >            # raise exception
>> >
>> > Now that I have gone async, I want to do the same with an asynchronous
>> > iterator.
>>
>
>
I might be a bit off-topic, but why don't you simply use cursor.rowcount?

For a pure iterator-based solution, I would do something like this (admitly
a bit cryptic, but iterator-based solutions often are :-) :

async def get_uniqu(ait):
    async for row in ait:
        break
    else:
        raise NotEnoughtRows()
    async for _ in ait:
        raise TooManyRows()
    return row



More information about the Python-list mailing list