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 01:14:48 -0700 Lines: 41 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 ETSlE4+6bz+t3HsaaheaIANbZZeeYJ1cNumupWuUe2Yg== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'received:209.85.223': 0.03; 'subject:Python': 0.05; 'exception.': 0.07; 'subject:How': 0.09; '__future__': 0.09; 'imports': 0.09; 'none.': 0.09; 'propagate': 0.09; 'exception': 0.13; 'def': 0.13; 'skip:f 30': 0.15; "%r'": 0.16; "(it's": 0.16; '12:57': 0.16; '2016': 0.16; 'async': 0.16; 'iterator,': 0.16; 'iterator.': 0.16; 'nudge': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'runtimeerror': 0.16; 'surprising': 0.16; 'wrote:': 0.16; 'work,': 0.21; 'suppose': 0.22; 'am,': 0.23; 'defined': 0.23; 'feb': 0.23; 'sat,': 0.23; 'import': 0.24; 'header:In-Reply-To:1': 0.24; "doesn't": 0.26; 'chris': 0.26; 'equivalent': 0.27; 'separate': 0.27; 'question': 0.27; 'message-id:@mail.gmail.com': 0.27; 'mind,': 0.29; 'pep': 0.29; 'subject:/': 0.30; 'work.': 0.30; 'code': 0.30; 'becomes': 0.30; 'supposed': 0.31; 'another': 0.32; 'generally': 0.32; 'point': 0.33; 'raising': 0.33; 'file': 0.34; 'gets': 0.35; 'received:google.com': 0.35; 'fresh': 0.35; 'returning': 0.35; 'but': 0.36; 'should': 0.36; 'instead': 0.36; 'there': 0.36; 'received:209.85': 0.36; 'subject:work': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'being': 0.37; 'desired': 0.37; 'received:209': 0.38; 'subject:the': 0.39; 'to:addr:python.org': 0.40; 'still': 0.40; 'future': 0.60; 'save': 0.60; 'behavior': 0.61; '20,': 0.66; 'future,': 0.70; 'special': 0.73; 'yourself': 0.73; 'await': 0.76; 'awaited': 0.84; 'discussion)': 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=2csMpgJoLz3j+90yKkB+mMegZzKI5zA94S2wXeqj454=; b=UkzTrh1V64YqlWLDzaLseZ8A/AAxU57LbOnVzhLl3l0Vo5uECO0vO4tjkE5ruQ48Zh YP3R5cMW9s+hDDpORPbT13A62N9eP8Daqk43kawHd3QLuZjzENothQS7JJKBV+Jbs3a2 6grwx4CroiRW+9zyXTneWmEWScJneFmH8/rkzYNTirtP5//h8PYY/rFTrJGCC7iX24JG UtEwBRicV8VY5+12WQEfxmDKywAlF8c+/3wc+6DD6cD/TjFVetqTRTmplkB64RKt3Z5J 68pmIyWvzJAy6CrDnfibBAS2NJ6FZlIvxdM0IzFb9c8jNfuGz8KQbxfstjjkoX2m8AXH mrOA== 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=2csMpgJoLz3j+90yKkB+mMegZzKI5zA94S2wXeqj454=; b=ZWibegOPVEjrkAuPJUVoIuDgif/H7oyDWB2w249HKktk5jroiuLNh3QPmLWOotUkR6 7TqnjOT/sWJwziToGjNtD1c+gHIHp0wDlCJ9bEfCRdqk6iQUvQICNegchQsJUnvc6LtE uNd9sXbjH3OmfCLDromQKNrJRSxTc190w2+7wbbZqz+Tw1OFEiuFlMRPlqrygaV3SVUq G8lzCQFoyFLzOD6Sq5EAQhOpkoDpJ4F+nB4eI/h9B+q3cTPhyC9oADWLwQRX4pkTaPWn c2p1KFnt91y97dUPRKysPD95aa+K5NyI+TZt9WytF2DuNdT73hB+n1xv0aqMKQ9Ap22k RhKA== X-Gm-Message-State: AG10YOS81ipsXIBLKxswrqvMdcSdeKnCcAxyxdcOiAz4xRxC7ZK5yNRksMsk+eQMuSciwN5xfiBYS43W4PoefA== X-Received: by 10.107.19.90 with SMTP id b87mr20759724ioj.11.1455956128058; Sat, 20 Feb 2016 00:15:28 -0800 (PST) In-Reply-To: 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:103249 On Sat, Feb 20, 2016 at 12:57 AM, Chris Angelico wrote: > On Sat, Feb 20, 2016 at 6:48 PM, Ian Kelly wrote: >> 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. > > Solved by PEP 479. Use "from __future__ import generator_stop" to save > yourself the pain. Nope. py> from __future__ import generator_stop py> import asyncio py> async def test_coro(): ... fut = asyncio.Future() ... fut.set_exception(StopIteration()) ... print('received %r' % await fut) ... py> list(test_coro().__await__()) received None [] I think because __future__ imports are per-file, and asyncio.Future.__iter__ is defined in a file outside my control that doesn't have the __future__ import. I suppose that when the generator_stop behavior becomes standard then it will work, but still that will just cause a RuntimeError to propagate instead of the desired StopIteration. It's not really that big a deal since there is a code smell to it, but it's surprising since intuitively StopIteration should have no special meaning to a PEP 492 coroutine (it's not an iterator, wink wink, nudge nudge), and the thing being awaited is a Future, which also doesn't intuitively look like an iterator. Note that if you just call Future.result(), then the exception propagates as expected; it's just awaiting it that doesn't work.