Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico 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: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de 7v7DdXiHJvmvZYBl+yMBBQWMJmMUtWrpNjh7qPwxtomA== Return-Path: 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: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:102288 On Sat, Jan 30, 2016 at 7:22 PM, Frank Millman 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