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 23:55:14 +1100 Lines: 34 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de zWoOgOBNCJymB6CRvIbJCAyT7ryAdgacNNFb8u3tjCjQ== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'yet.': 0.03; 'wrapper': 0.07; 'cc:addr:python-list': 0.09; 'default:': 0.09; 'situation.': 0.09; 'snippet': 0.09; 'assume': 0.11; 'jan': 0.11; 'def': 0.13; '2016': 0.16; 'async': 0.16; 'cc:name:python': 0.16; 'exposes': 0.16; 'fancy': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'iterable': 0.16; 'iterables': 0.16; 'object()': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'sentinel': 0.16; 'value:': 0.16; 'wrote:': 0.16; 'library': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'kevin': 0.23; 'sat,': 0.23; 'header:In-Reply-To:1': 0.24; "doesn't": 0.26; 'equivalent': 0.27; 'least': 0.27; 'message-id:@mail.gmail.com': 0.27; 'interface': 0.29; 'raise': 0.29; 'fixed': 0.31; 'curious': 0.33; 'that,': 0.34; 'received:google.com': 0.35; 'next': 0.35; 'question,': 0.35; 'but': 0.36; 'skip:i 20': 0.36; 'received:209.85': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'one,': 0.37; 'received:209.85.213': 0.37; 'received:209': 0.38; 'rather': 0.39; 'address': 0.61; 'avoid': 0.61; 'default': 0.61; '30,': 0.63; 'believe': 0.66; 'await': 0.76; 'chrisa': 0.84; 'idiom': 0.84; 'original.': 0.84; 'ultimately,': 0.84; 'url:show': 0.84; 'to:none': 0.91; 'comment.': 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=JdEaWGzX9pgXb6qhmWvz61h1YKdxV7oozdSq+FMIPGg=; b=G9t3h2FHM8IxR1RpORvmEbukfOzvcxGCQ5u2KpnXhGXKvFR5J9N84OJlcJvbEzw3/O yH/Xa4r2JebzUgTjhgEGy+WVZFjsGBIty8ZtVXVQEiLnCm++UyxHnMM5WvkMzfIncXt+ aoCLDkwZ6EOpssdEIOZ59y59j1Soe4TpaONucgTEUTA5X1vcf+Z4oUU6unuYeelONiox TYnTWbwFxivlRRXMsuJ8FodFsjhy3nGFQ6tH9iERHNx2LR/Wpz9NSxCQXNNAjK1jqkdE owN9rAicX7rs+MnbQmE+1Y6ILYkLhzQz+OrmUJJuhNgY6xvV9fC7j4vAq48Plu8xgClA rieg== 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=JdEaWGzX9pgXb6qhmWvz61h1YKdxV7oozdSq+FMIPGg=; b=BrS13Hc4MFZVbLt7KqMxOEJbNeW77dxncH5iz/JkQpJFVY6rS3iRovE38ot78dObv2 41qY1d7CkDoI47bXd8qJA5/FZXt6jv6/LcOPGwEjUoKo6X1GjQcJIT4sv+DuAS6+NymN Wg4R3dJn7uqMtku1QQZb6esjUd32vp8a4GT2sTNoEDSGlX280u8oDPt7r4YsXuZvJ8SN Z/x05zCHkZT2UXfSz+CJWniEUxY7sTR97uTdyFNQ9XL7su2wQMIpTjfMfQ8n8yPLjtTr SxavWyOUp3+DvDN9Ls/imiAiFY32AP5ayBUlRISQ0/X/jNHrFYhRJSpo+ZmnkiY5oxeR 0UHA== X-Gm-Message-State: AG10YORLmyK4v4X/FkkEAHcS6CUQzEuy3PugHTkGJVzPasbyoOiF15JE5XOVJLmnC9eOok2YrSOnI24N/07p0Q== X-Received: by 10.50.66.179 with SMTP id g19mr2308662igt.94.1454158514613; Sat, 30 Jan 2016 04:55:14 -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:102301 On Sat, Jan 30, 2016 at 11:35 PM, Kevin Conway wrote: > 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__()'. Curious idiom for the one-or-two-arg situation. Any particular reason not to use the classic sentinel object model? _SENTINEL = object() async def anext(iterable, default=_SENTINEL): ... if default is not _SENTINEL: return default Or if you want to avoid that, at least take iterable as a fixed arg: async def anext(iterable, *default): if len(default) > 1: TypeError ... if default: return default[0] Also curious is that you raise a new StopAsyncIteration from the original one, rather than just reraising the original. I assume there's a reason for that, but it doesn't have a comment. ChrisA