Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Ian Kelly Newsgroups: comp.lang.python Subject: Re: How the heck does async/await work in Python 3.5 Date: Sat, 20 Feb 2016 00:48:31 -0700 Lines: 45 Message-ID: References: <56c7d145$0$1597$c3e8da3$5496439d@news.astraweb.com> <9d968a2e-f23c-4c93-979d-43dfa610c343@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de ZzvOJoQGrdGcngr79RpXhAMwCoGU17EOckIqUHwzF7gw== 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; 'received:209.85.223': 0.03; 'subject:Python': 0.05; 'method.': 0.05; 'exception.': 0.07; 'subject:How': 0.09; 'abstraction': 0.09; 'generators': 0.09; 'iterate': 0.09; 'none.': 0.09; 'propagate': 0.09; 'semantics': 0.09; 'underlying': 0.09; 'exception': 0.13; 'def': 0.13; "*isn't*": 0.16; '2016': 0.16; 'coroutines': 0.16; 'entirely.': 0.16; 'generators.': 0.16; "guido's": 0.16; 'iterable': 0.16; 'iteration.': 0.16; 'iterator': 0.16; 'iterator.': 0.16; 'mistake.': 0.16; 'nevertheless': 0.16; 'overloaded': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'symmetric': 0.16; 'wrote:': 0.16; '2001': 0.18; 'basically': 0.18; "aren't": 0.22; 'greg': 0.22; 'minor': 0.22; 'rid': 0.22; 'feb': 0.23; 'implemented': 0.24; 'thus': 0.24; 'header:In-Reply-To:1': 0.24; "doesn't": 0.26; 'sense': 0.26; 'equivalent': 0.27; 'fri,': 0.27; 'separate': 0.27; 'question': 0.27; 'message-id:@mail.gmail.com': 0.27; 'looks': 0.29; 'mind,': 0.29; 'pep': 0.29; 'subject:/': 0.30; 'work.': 0.30; 'supposed': 0.31; 'another': 0.32; 'implement': 0.32; 'generally': 0.32; 'language.': 0.32; 'maybe': 0.33; 'point': 0.33; 'choosing': 0.33; 'raising': 0.33; 'gets': 0.35; 'received:google.com': 0.35; 'clear': 0.35; 'fresh': 0.35; 'returning': 0.35; 'expected': 0.35; 'step': 0.36; 'but': 0.36; 'too': 0.36; 'should': 0.36; 'instead': 0.36; 'received:209.85': 0.36; 'keyword': 0.36; 'subject:work': 0.36; 'to:addr:python- list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'received:209': 0.38; 'wrong': 0.38; 'subject:the': 0.39; 'rather': 0.39; 'to:addr:python.org': 0.40; 'still': 0.40; 'future': 0.60; 'ever': 0.60; 'hope': 0.61; 'skip:n 10': 0.62; 'more': 0.63; 'great': 0.63; 'series': 0.65; "they're": 0.66; 'future.': 0.67; 'future,': 0.70; 'sound': 0.72; '"yield': 0.84; 'complaint': 0.84; 'discussion)': 0.84; 'distinguish': 0.84; 'down:': 0.84; 'from"': 0.84; 'to:name:python': 0.84; 'awaiting': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=G9LdTX1qtyonzWkGoMNpRPOLipNf1do0687uE2WJMmk=; b=aFvw26RyOlTPDvqNSg6HzDSaw88JEDTBIV/QftcGwGLECOPGiZkyP52+XRukN3t5U8 ULhn6f1JE76J3TKrTpKe/FWey4D3YWDMEdAO3WpOmUud3Jznuxi3V3QxloSYtve5wU6F lg3rxXpI3kS9ZI/EJaft0z0LLNx8TKvGAra6odbu8n9gGDZn2Cc2Y4as11iNA0nKkcEg RkyPAS7uvbjes5fpmnKrW3Rtz/tFANHDPclxjE+R6VOJxFsMqjpcaWZIdSHj6Zvhat/9 2wpxUQS+/lzbUbM24WQkjPtFq5eVifRKJXDHZCVcn12FcU+Y1C2ZFGp8ZF31QBdZ6J80 x5Qg== 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:from:date :message-id:subject:to:content-type; bh=G9LdTX1qtyonzWkGoMNpRPOLipNf1do0687uE2WJMmk=; b=c/2ssA2ZHN5Sgj/YDKtnVfeTkwBX0wJczzS1DO95a7fMgDJWBbSMUIuWHOiiaXbt0d dWNZ8Og7GhiOqSSIcUxlbflv8RAOPTPD73b0qqV70Wet9dzGhx/mlG3sUlVzeDCAzSYN wbbiavEh/FWcGVHov9mUBrmnEBvMZAmMyayCMc5wdD5ly2sYKQZmZ4BmlIlIjXvcGWSL uwmOxFslvr6RGkhCgCMlPpNa3s9GctfZq4jaTEwpntBmt8eNJNG488v5NyDbK/9vddzj rVhDqqVh0szyM338RhszeupXnSmGVbZAEu2bWUzct+MoK2hcyWTQtwvTh5WBJKIAmNsl wvmQ== X-Gm-Message-State: AG10YOQzStllnPaef6SvwXvIRbpxMn4gZEqR0oq2S664dki0Pgs0NI0V+nqF67Eb8SLPs+eTQUeVEOZjRvB5gQ== X-Received: by 10.107.185.214 with SMTP id j205mr18890535iof.111.1455954551159; Fri, 19 Feb 2016 23:49:11 -0800 (PST) In-Reply-To: <9d968a2e-f23c-4c93-979d-43dfa610c343@googlegroups.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21rc2 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:103244 On Fri, Feb 19, 2016 at 10:24 PM, Rustom Mody wrote: > Less snarkily looks like a series of bolt-ons after bolt-ons > > IMHO Guido's (otherwise) uncannily sound intuitions have been wrong right from > 2001 when he overloaded def for generators. > And after that its been slippery-slope down: reusing generator-yield (statement) > for coroutine-yield (expression) > Most recently choosing these async-await keywords instead of the more symmetric > suggestions of Greg Ewing Two out of three of those are fine. IMO generators should have required a separate keyword to distinguish them from ordinary functions, but it's a minor complaint and they're otherwise a great addition to the language. I do think it's pretty clear at this point though that PEP 342 was a mistake. We should never have introduced generator-based coroutines; it's an abstraction with too much leakage. It doesn't make sense that generator-based coroutines implement the iterator protocol, because there's no reason to ever try to iterate over them. It also doesn't make sense to ever iterate over a Future, and yet asyncio Futures, which aren't even coroutines, are nevertheless *forced* to be iterable just because that's how coroutines work. Otherwise you couldn't "yield from" a Future. As another point that happens to be fresh in my mind, awaiting a Future on which an exception gets set is supposed to propagate the exception. I recently found that this breaks if the exception in question happens to be StopIteration (granted not one that should generally be allowed to propagate anyway, but that's a separate discussion) for the simple reason that raising StopIteration in a generator is equivalent to returning None. The awaiting coroutine thus gets a non-exceptional result of None rather than the expected exception. The irritating thing to me is that this even plagues PEP 492 coroutines using "await" rather than "yield from", because the two are basically the same under the covers. The former gets an iterator by calling __await__ instead of __iter__, but it's still implemented using an iterator. If you look at the asyncio.Future implementation, __await__ is actually just a synonym of the __iter__ method. My hope is that someday we can get rid of PEP 342 coroutines entirely. Then maybe we can get a coroutine implementation that's actually sane. PEP 492 with async/await and non-iterable semantics is a step in the right direction, but ultimately I think we also need an underlying implementation that *isn't* fundamentally based on iteration.