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


Groups > comp.lang.python > #102287

Re: Cannot step through asynchronous iterator manually

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From "Frank Millman" <frank@chagford.com>
Newsgroups comp.lang.python
Subject Re: Cannot step through asynchronous iterator manually
Date Sat, 30 Jan 2016 10:22:20 +0200
Lines 57
Message-ID <mailman.117.1454142160.2338.python-list@python.org> (permalink)
References <n8hjmt$b8n$1@ger.gmane.org> <CALwzid=sSDSm8hdAN+ORJ54A_jEu9Wc8103iqGKAah8mrj-TXw@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; format=flowed; charset="iso-8859-1"; reply-type=original
Content-Transfer-Encoding 7bit
X-Trace news.uni-berlin.de zGGKGYKkUt/CX8yErwJm7gtdNB4Ek/6UzoBXAQxPFjEQ==
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'higher- level': 0.09; 'loop.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'rows': 0.09; 'python': 0.10; 'jan': 0.11; 'exception': 0.13; '...)': 0.16; '2016': 0.16; 'async': 0.16; 'iterator': 0.16; 'iterator,': 0.16; 'iterator.': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'returned,': 0.16; 'row': 0.16; 'skip:n 70': 0.16; 'wrote:': 0.16; 'case.': 0.18; 'try:': 0.18; 'suggested': 0.20; 'pass': 0.22; 'select': 0.23; 'wrote': 0.23; 'header:In-Reply-To:1': 0.24; 'discussion': 0.24; 'header:X-Complaints-To:1': 0.26; "skip:' 10": 0.28; 'raise': 0.29; 'usually': 0.33; 'except': 0.34; 'exist': 0.35; 'expected': 0.35; 'step': 0.36; 'but': 0.36; 'there': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'desired': 0.37; 'missing': 0.37; 'test': 0.39; 'does': 0.39; 'to:addr:python.org': 0.40; 'entire': 0.61; 'more': 0.63; 'times': 0.63; 'statement,': 0.66; 'course.': 0.67; 'liked': 0.67; 'frank': 0.72; 'special': 0.73; "'for'": 0.84; 'chrisa': 0.84; 'construct': 0.84
X-Injected-Via-Gmane http://gmane.org/
X-Gmane-NNTP-Posting-Host 197.86.205.12
In-Reply-To <CALwzid=sSDSm8hdAN+ORJ54A_jEu9Wc8103iqGKAah8mrj-TXw@mail.gmail.com>
X-MSMail-Priority Normal
Importance Normal
X-Newsreader Microsoft Windows Live Mail 15.4.3502.922
X-MimeOLE Produced By Microsoft MimeOLE V15.4.3502.922
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:102287

Show key headers only | View raw


"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

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


Thread

Re: Cannot step through asynchronous iterator manually "Frank Millman" <frank@chagford.com> - 2016-01-30 10:22 +0200

csiph-web