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


Groups > comp.lang.python > #102288

Re: Cannot step through asynchronous iterator manually

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Subject Re: Cannot step through asynchronous iterator manually
Date Sat, 30 Jan 2016 19:27:59 +1100
Lines 37
Message-ID <mailman.118.1454142488.2338.python-list@python.org> (permalink)
References <n8hjmt$b8n$1@ger.gmane.org> <CALwzid=sSDSm8hdAN+ORJ54A_jEu9Wc8103iqGKAah8mrj-TXw@mail.gmail.com> <n8hrs1$orn$1@ger.gmane.org>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de 7v7DdXiHJvmvZYBl+yMBBQWMJmMUtWrpNjh7qPwxtomA==
Return-Path <rosuav@gmail.com>
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; 'none:': 0.05; 'cc:addr:python-list': 0.09; 'none.': 0.09; 'jan': 0.11; 'exception': 0.13; '(assuming': 0.16; '...)': 0.16; '2016': 0.16; 'abusing': 0.16; 'async': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'iterator.': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'row': 0.16; 'sentinel': 0.16; 'wrote:': 0.16; 'try:': 0.18; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'suggested': 0.20; 'not,': 0.22; 'sat,': 0.23; 'header:In-Reply-To:1': 0.24; 'discussion': 0.24; 'message-id:@mail.gmail.com': 0.27; 'directly,': 0.29; 'raise': 0.29; 'work.': 0.30; "can't": 0.32; 'option.': 0.33; 'except': 0.34; 'received:google.com': 0.35; 'could': 0.35; 'exist': 0.35; 'but': 0.36; 'received:209.85': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'method': 0.37; 'received:209.85.213': 0.37; 'received:209': 0.38; 'does': 0.39; '30,': 0.63; 'liked': 0.67; 'frank': 0.72; 'chrisa': 0.84; "it'd": 0.84; 'to:none': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=Xo/hpK2wcbmLk+fHKRiFi328F7UsphDaHUB/4LNBF1I=; b=Y2gmLFAEQVNXUacRt7THcaXV442D4JMl9WEYqnIyowsPfVpVuyEC/kkuHgAcUq4vvk duFFWVyKox02DCZm7N3tlXFsP3O7lAinAla5aZdFAR8s4SH4Irkr8ojoJZlPKfPgxxSc rG7B/peWRp2MlT6b/061q9HSUjf4jUlec6p6/siIVt/pvb+1nWqKD9XjGuHsZ8QWHQ7l X9olaqBNk2DRUVZ/t/XhWDqlQQwqTOv6nFpTdJH4NG7EpQ11Z1yi4Rk2cYBXZUjHtag4 Tp5HSsRWCH21wEiP6DwqpFp1sJaFvcEGoCSKZa7nx7uY7fzM5c5fo8FwivNZVLvRDlMP wpHw==
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:cc:content-type; bh=Xo/hpK2wcbmLk+fHKRiFi328F7UsphDaHUB/4LNBF1I=; b=L7TJrfATHaTqsqe0XkOmYsl2zAfeQf0Dkme4mPNdXHrhOn3HJ+HPS+A+wYaT5XHneU jmn4ks2x/2Lwiknc8odH8At/DcO4fIV/qVS/zaEusKeEbkCVpU07zkDQaNoJ4aDtOvMX 6TBLvOkpEfWGcGcGr6Tb5JTIOvV/19CJmyXFWHdecJFcuChwUxZMqWweLtIpw4cB6R+f bxnDXiS5KDJeVAF+MDIhwMc4mOHYEgPauIthc0mkrVTpD6rwIPnnKE/XeJSX5B1CWGGR 3CuX1lxmEMFlKgnFIlVkayzoxgoDFmbe/CtrE5F82v95VfsvxVw017GTHgxEb8NnmPQ9 yZfQ==
X-Gm-Message-State AG10YOQ1YgppAxVCoSXP1nr5MJVqV2J5EJ4RmTKk+6sM8s1oL6GMrtl0uRBj9gVOpg06NtAcKe4ZMuvUkBOwWA==
X-Received by 10.50.66.179 with SMTP id g19mr1464838igt.94.1454142479771; Sat, 30 Jan 2016 00:27:59 -0800 (PST)
In-Reply-To <n8hrs1$orn$1@ger.gmane.org>
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:102288

Show key headers only | View raw


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

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


Thread

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

csiph-web