Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #102298

Re: Cannot step through asynchronous iterator manually

From Kevin Conway <kevinjacobconway@gmail.com>
Newsgroups comp.lang.python
Subject Re: Cannot step through asynchronous iterator manually
Date 2016-01-30 12:35 +0000
Message-ID <mailman.124.1454157430.2338.python-list@python.org> (permalink)
References (1 earlier) <CALwzid=sSDSm8hdAN+ORJ54A_jEu9Wc8103iqGKAah8mrj-TXw@mail.gmail.com> <n8hrs1$orn$1@ger.gmane.org> <CAPTjJmoAmVNTCKq7QYaDRNQ67Gcg9TxSXYXCrY==S9Djjna_rA@mail.gmail.com> <n8i4j9$lb4$1@ger.gmane.org> <CAGqiJR8yUdd1u7j0YHS-He_v4uUT-ui=PpiX=n_G=ntt8ZnTpg@mail.gmail.com>

Show all headers | View raw


To address the original question, I don't believe a next() equivalent for
async iterables has been added to the standard library yet. Here's an
implementation from one of my projects that I use to manually get the next
value: https://bpaste.net/show/e4bd209fc067. It exposes the same interface
as the synchronous next(). Usage:

    await anext(some_async_iterator)

Ultimately, it's a fancy wrapper around the original snippet of 'await
iterator.__anext__()'.


On Sat, Jan 30, 2016 at 6:07 AM Maxime S <maxischmeii@gmail.com> wrote:

> 2016-01-30 11:51 GMT+01:00 Frank Millman <frank@chagford.com>:
>
> > "Chris Angelico"  wrote in message
> > news:CAPTjJmoAmVNTCKq7QYaDRNQ67Gcg9TxSXYXCrY==S9Djjna_rA@mail.gmail.com.
> ..
> >
> >
> >> On Sat, Jan 30, 2016 at 7:22 PM, Frank Millman <frank@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
> --
> https://mail.python.org/mailman/listinfo/python-list
>

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Cannot step through asynchronous iterator manually Kevin Conway <kevinjacobconway@gmail.com> - 2016-01-30 12:35 +0000

csiph-web