Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Kevin Conway Newsgroups: comp.lang.python Subject: Re: Cannot step through asynchronous iterator manually Date: Sat, 30 Jan 2016 12:35:13 +0000 Lines: 60 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de 5W3ryL+8Gzo+LPsmCvUA8AszWtjPU9jUfRlloaBH5A9g== 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; 'yet.': 0.03; 'wrapper': 0.07; 'cc:addr:python-list': 0.09; 'snippet': 0.09; 'jan': 0.11; ':-)': 0.12; 'exception': 0.13; 'def': 0.13; '...)': 0.16; '2016': 0.16; 'async': 0.16; 'cc:name:python': 0.16; 'exposes': 0.16; 'fancy': 0.16; 'iterables': 0.16; 'iterator.': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'row': 0.16; 'skip:n 70': 0.16; 'value:': 0.16; 'wrote:': 0.16; 'try:': 0.18; '>': 0.18; 'email addr:gmail.com>': 0.18; 'library': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'suggested': 0.20; 'to:2**1': 0.21; 'bit': 0.23; 'wrote': 0.23; 'sat,': 0.23; 'header :In-Reply-To:1': 0.24; 'discussion': 0.24; 'equivalent': 0.27; 'message-id:@mail.gmail.com': 0.27; 'interface': 0.29; 'solution,': 0.29; 'raise': 0.29; 'url:mailman': 0.30; 'url:python': 0.33; 'url:listinfo': 0.34; 'except': 0.34; 'received:google.com': 0.35; 'next': 0.35; 'exist': 0.35; 'question,': 0.35; 'something': 0.35; 'but': 0.36; 'skip:i 20': 0.36; 'url:org': 0.36; 'received:209.85': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'received:209.85.213': 0.37; 'received:209': 0.38; 'why': 0.39; 'does': 0.39; 'url:mail': 0.40; 'address': 0.61; 'skip:n 10': 0.62; '30,': 0.63; 'believe': 0.66; 'liked': 0.67; 'frank': 0.72; 'await': 0.76; 'chrisa': 0.84; 'ultimately,': 0.84; 'url:show': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-type; bh=HuaRD0YyKXznEC5BBaB8/T3XwQ730/zH4Zljm7WiwwM=; b=ZKUpxj9g3t8FZ7WpziFk6Br+ojBNZM/fVt5TL1EKXSbH+fdky0B0n/4joShtkoADIJ Jgle4moms2QcSPUuRRn6WVic5luIpn7gpx9Rf63ONGHSyuVWVlDMU15nM98c6x/Mfw17 s5UOeSIkrpjEtfJ+X4YWnSrlHn2DWpT7FBWLNEWgOOhuux8rNUo64vstGuXzWZhEoUhg AgcQRe/eizIL0IuFFPmTf00RBs5CSDU11dNbe3KUZ97SrMwN6a/T+subCXWwjYOCRW8k +1WvxhOHMDelgYG5w8ZaRfYyQq+/VPNgCzOINAz+On/qqAwobf2CdzDdoFgxve43i5do Oinw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-type; bh=HuaRD0YyKXznEC5BBaB8/T3XwQ730/zH4Zljm7WiwwM=; b=eobm1jTHSAMYL1enMR3ZAznDdiqHIWXEXxq0sGyCgfl0+PS+u6gj9KUL/rg2HbQfM/ Z02X0E7LjqoL3Txo2kFn9Uf+dJEXi2h8D6yZ96xwSSjcVYcRcVqKR592Xrr0ub6FkLan UsdVBXIRGuASKU4H8BYdYtYV/wywG8iGUHU8y+OCycFZTV1A6CCJED0hENhw25IVSeBG vt15hl3v+k3eEnAqDOFUTIJQc4J5Slof2vxtSa/AHtYrTkqYssDZ7zxGy6NqNKQ2JQjd 02MDv8+wVkW5kVjI1c/vO99uExcQn+PXxgJcohgUS3pnOmRx64cBs/m+R7KpomqW7SOk PwgA== X-Gm-Message-State: AG10YOTVXEwRcyMXI3RxSXGbVwBh0eolkoAxqtKbBH4hx6302sKfmig0YEIZyC3t8G0N13RRQnP4ti+TsHSsfQ== X-Received: by 10.50.87.36 with SMTP id u4mr1694285igz.67.1454157323116; Sat, 30 Jan 2016 04:35:23 -0800 (PST) In-Reply-To: X-Mailman-Approved-At: Sat, 30 Jan 2016 07:37:08 -0500 X-Content-Filtered-By: Mailman/MimeDel 2.1.20+ 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:102298 To address the original question, I don't believe a next() equivalent for async iterables has been added to the standard library yet. Here's an implementation from one of my projects that I use to manually get the next value: https://bpaste.net/show/e4bd209fc067. It exposes the same interface as the synchronous next(). Usage: await anext(some_async_iterator) Ultimately, it's a fancy wrapper around the original snippet of 'await iterator.__anext__()'. On Sat, Jan 30, 2016 at 6:07 AM Maxime S wrote: > 2016-01-30 11:51 GMT+01:00 Frank Millman : > > > "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. > >> > > > > > I might be a bit off-topic, but why don't you simply use cursor.rowcount? > > For a pure iterator-based solution, I would do something like this (admitly > a bit cryptic, but iterator-based solutions often are :-) : > > async def get_uniqu(ait): > async for row in ait: > break > else: > raise NotEnoughtRows() > async for _ in ait: > raise TooManyRows() > return row > -- > https://mail.python.org/mailman/listinfo/python-list >