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


Groups > comp.lang.python > #102288 > unrolled thread

Re: Cannot step through asynchronous iterator manually

Started byChris Angelico <rosuav@gmail.com>
First post2016-01-30 19:27 +1100
Last post2016-01-30 19:27 +1100
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Cannot step through asynchronous iterator manually Chris Angelico <rosuav@gmail.com> - 2016-01-30 19:27 +1100

#102288 — Re: Cannot step through asynchronous iterator manually

FromChris Angelico <rosuav@gmail.com>
Date2016-01-30 19:27 +1100
SubjectRe: Cannot step through asynchronous iterator manually
Message-ID<mailman.118.1454142488.2338.python-list@python.org>
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.

Here's a crazy option. (Assuming that a row can't be None. If not, use
a unique sentinel object.)

cur.execute(whatever)
have_row = None
async for row in cur:
    if have_row is not None:
        raise TooManyRows
    have_row = row
if have_row is None:
    raise NoRowFound

It's kinda abusing the loop construct, but it'd work. Alternatively,
you could call the dunder method directly, but that feels dirty.
Dunders are for defining, not calling.

ChrisA

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web