Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: "Frank Millman" Newsgroups: comp.lang.python Subject: Re: Cannot step through asynchronous iterator manually Date: Sat, 30 Jan 2016 12:51:16 +0200 Lines: 55 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; format=flowed; charset="UTF-8"; reply-type=original Content-Transfer-Encoding: 8bit X-Trace: news.uni-berlin.de Ot3ZdlIu5rqe3qQYFOrxhg7GWwATB38fuPpQkGmaIr9Q== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'none:': 0.05; 'none.': 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; 'jan': 0.11; ':-)': 0.12; 'exception': 0.13; '(assuming': 0.16; '...)': 0.16; '2016': 0.16; 'async': 0.16; 'doesn\xe2\x80\x99t': 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; 'row': 0.16; 'sentinel': 0.16; 'skip:n 70': 0.16; 'wrote:': 0.16; 'try:': 0.18; 'variable': 0.18; 'changes': 0.20; 'suggested': 0.20; 'not,': 0.22; 'wrote': 0.23; 'sat,': 0.23; 'slightly': 0.23; 'header:In-Reply-To:1': 0.24; 'discussion': 0.24; 'header:X -Complaints-To:1': 0.26; 'idea': 0.28; 'really,': 0.29; 'raise': 0.29; "can't": 0.32; 'run': 0.33; 'option.': 0.33; 'except': 0.34; 'exist': 0.35; 'false': 0.35; 'but': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'does': 0.39; 'to:addr:python.org': 0.40; '30,': 0.63; 'here': 0.66; 'liked': 0.67; 'frank': 0.72; 'chrisa': 0.84; 'variation': 0.84 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: 197.86.205.12 In-Reply-To: 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:102292 "Chris Angelico" wrote in message news:CAPTjJmoAmVNTCKq7QYaDRNQ67Gcg9TxSXYXCrY==S9Djjna_rA@mail.gmail.com... > > 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 > Not so crazy :-) If Ian doesn’t come up with a better idea I will run with it. Here is a slight variation - just variable name changes really, but I think it is slightly easier to read - found = False async for row in cur: if found: raise TooManyRows found = True if found: process row else: no rows found Frank