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


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

Re: Cannot step through asynchronous iterator manually

Started by"Frank Millman" <frank@chagford.com>
First post2016-01-30 10:22 +0200
Last post2016-01-30 10:22 +0200
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 "Frank Millman" <frank@chagford.com> - 2016-01-30 10:22 +0200

#102287 — Re: Cannot step through asynchronous iterator manually

From"Frank Millman" <frank@chagford.com>
Date2016-01-30 10:22 +0200
SubjectRe: Cannot step through asynchronous iterator manually
Message-ID<mailman.117.1454142160.2338.python-list@python.org>
"Ian Kelly"  wrote in message 
news:CALwzid=sSDSm8hdAN+ORJ54A_jEu9Wc8103iqGKAah8mrj-TXw@mail.gmail.com...

> On Jan 29, 2016 11:04 PM, "Frank Millman" <frank@chagford.com> wrote:
> >
> > Hi all
> >
> > To loop though an iterator one usually uses a higher-level construct 
> > such
as a 'for' loop. However, if you want to step through it manually you can
do so with next(iter).
> >
> > I expected the same functionality with the new 'asynchronous iterator' 
> > in
Python 3.5, but I cannot find it.
> >
> > I can achieve the desired result by calling 'await aiter.__anext__()',
but this is clunky.
> >
> > Am I missing something?
>
> async for x in aiter:
>     pass
>
> Can only be used inside a coroutine, of course.

I know that you can use this to loop through the entire iterator, but I have 
a special case.

There are times when I want to execute a SELECT statement, and test for 
three possibilities -
    - if no rows are returned, the object does not exist
    - if one row is returned, the object does exist
    - if more that one row is returned, raise an exception

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.

Frank

[toc] | [standalone]


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


csiph-web