Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #103254

Re: How the heck does async/await work in Python 3.5

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Subject Re: How the heck does async/await work in Python 3.5
Date Sat, 20 Feb 2016 19:49:04 +1100
Lines 73
Message-ID <mailman.17.1455958153.13884.python-list@python.org> (permalink)
References <mailman.224.1455746935.22075.python-list@python.org> <56c7d145$0$1597$c3e8da3$5496439d@news.astraweb.com> <9d968a2e-f23c-4c93-979d-43dfa610c343@googlegroups.com> <CALwzidnVrNmhbRhkoiCkwZbqw+7h8gd_TY5HLUMNs1ROcHzQhQ@mail.gmail.com> <CAPTjJmoFiPtoo7oNopUqsrsQJhZWySA_hGEXbuXbQxKgmPh44A@mail.gmail.com> <CALwzidnQWwtqgLW-dzX4gOrRzO_4EXLzRBCvnczdGp0T0LU=oA@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de MgOE0xROlv08+wKoJ6zxeQ9O85SHdpffRqyug05hO3yQ==
Return-Path <rosuav@gmail.com>
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; 'received:209.85.223': 0.03; 'subject:Python': 0.05; 'none,': 0.05; 'defines': 0.07; 'exception.': 0.07; 'indicated': 0.07; 'raises': 0.07; 'cc:addr :python-list': 0.09; 'subject:How': 0.09; '__future__': 0.09; 'fixed,': 0.09; 'imports': 0.09; 'issue?': 0.09; 'none.': 0.09; 'normally,': 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; 'cc:name:python': 0.16; 'code).': 0.16; 'directive': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'iterator,': 0.16; 'iterator.': 0.16; 'nudge': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'runtimeerror': 0.16; 'suite.': 0.16; 'surprising': 0.16; 'wrote:': 0.16; 'mechanism': 0.18; 'hack': 0.18; '>>>': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'tracker': 0.20; 'work,': 0.21; 'latter': 0.22; 'minor': 0.22; 'suppose': 0.22; 'terminate': 0.22; 'am,': 0.23; 'defined': 0.23; 'bit': 0.23; 'seems': 0.23; 'feb': 0.23; 'sat,': 0.23; 'import': 0.24; 'header :In-Reply-To:1': 0.24; "doesn't": 0.26; 'example': 0.26; '(which': 0.26; 'chris': 0.26; 'error': 0.27; 'equivalent': 0.27; 'separate': 0.27; 'question': 0.27; 'message-id:@mail.gmail.com': 0.27; 'change,': 0.27; 'yield': 0.27; 'function': 0.28; 'behaviour': 0.29; 'mind,': 0.29; 'pep': 0.29; 'return;': 0.29; 'raise': 0.29; 'subject:/': 0.30; 'work.': 0.30; 'code': 0.30; 'becomes': 0.30; 'call.': 0.30; 'guess': 0.31; 'supposed': 0.31; 'skip:s 30': 0.31; 'another': 0.32; "can't": 0.32; 'generally': 0.32; "who's": 0.32; 'maybe': 0.33; 'point': 0.33; 'instead,': 0.33; 'raised': 0.33; 'raising': 0.33; 'case,': 0.34; 'file': 0.34; 'gets': 0.35; 'received:google.com': 0.35; 'could': 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; 'pm,': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'being': 0.37; 'desired': 0.37; 'seem': 0.37; 'difference': 0.38; 'received:209': 0.38; 'test': 0.39; 'subject:the': 0.39; 'still': 0.40; 'future': 0.60; 'save': 0.60; 'your': 0.60; 'behavior': 0.61; 'per': 0.62; 'more': 0.63; 'between': 0.65; '20,': 0.66; 'here': 0.66; 'future,': 0.70; 'special': 0.73; 'yourself': 0.73; 'await': 0.76; 'actually,': 0.84; 'awaited': 0.84; 'chrisa': 0.84; 'discussion)': 0.84; 'to:none': 0.91; 'awaiting': 0.91; 'tricky': 0.93
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=sy06K/F6YgsH3flaG0JNt0UXKMWRtQGeVup7haC79pI=; b=suRVlvbcBrmypYRGgeDnLngB3uVH5jYqWTb0nuUhbQN1Gw6JZB3teIECua73r+2Rqe kzFMyyVVUTp7nSwODnD678GWYDd8g+Mz14jujW/ycuZNUsUVOUfVJDH7V/zUDk8I0wMI 8akJIlV3GnyAIn+kgT1wJrZbB+E6nwGaIk5Hnk64ylaBjP+81bkxl8Cj42FP+5PLnkdw FsPzUtRC2Xrnj8kDUZTlExwE1Y6RAPAk5jwR92rN/wFNhnJcbIvuReppm+jpws2K9NjQ mqD536z7q5QkHOzQNOdYBfrc+ITGdaTmSWsLvWdQNRb0fcp6vONA5ZPgJFJZI7F9KZEb Ia/g==
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=sy06K/F6YgsH3flaG0JNt0UXKMWRtQGeVup7haC79pI=; b=dgasiyGKsFZ318vf9pS8ABCPVWi6eFLk0FVWnDWkqw9PnWxNiqdoe3+10b4XPGzAqM attmflfiV/N34szlQxiGc9JNjPlmapEs2fR89nBd47BMOXHCYRcH8KZ0XH2q1rgfIpaG mkaYWgnHo0Qyivbse6j33FzBaFas6zML07YeXrDo35bXgi9/uC0Je8yVVHM6jsy5+6Xl A0HR4vcI7RFYZN3pWXvTAbfZC+R0qTtBg/lwLrSrXU2+6eyEjC2dxJeQx14xvEeS+7l3 NlsFsEqIYjHFvyE8i2BXdwD8vbxGiNNSy2E4sdoSc9+svvPgDKyH5e7KF3ageB9SJGld zs7Q==
X-Gm-Message-State AG10YOR17ecsyQRPLa0q+0wRQ1DBHUIKU9LiLyUPRqyaOTbDMZh9LsomElaAdePbfEcLKkZ2qDgvwuxlWEN75Q==
X-Received by 10.107.14.73 with SMTP id 70mr18306888ioo.31.1455958144469; Sat, 20 Feb 2016 00:49:04 -0800 (PST)
In-Reply-To <CALwzidnQWwtqgLW-dzX4gOrRzO_4EXLzRBCvnczdGp0T0LU=oA@mail.gmail.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 <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:103254

Show key headers only | View raw


On Sat, Feb 20, 2016 at 7:14 PM, Ian Kelly <ian.g.kelly@gmail.com> wrote:
> On Sat, Feb 20, 2016 at 12:57 AM, Chris Angelico <rosuav@gmail.com> wrote:
>> On Sat, Feb 20, 2016 at 6:48 PM, Ian Kelly <ian.g.kelly@gmail.com> 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.

You need the future directive in the file that defines the function
that raises, so I guess you'd need to apply that to an asyncio call.
The tricky bit here is that it's a backward compatibility change, but
since asyncio is flagged provisional, I suspect the future directive
could be added (anyone who's depending on
set_exception(StopIteration()) to terminate without an exception will
have to change code).

Actually, that mightn't be a bad thing. Maybe raise that as a tracker
issue? I just tested, and slapping "from __future__ import
generator_stop" at the top of Lib/asyncio/futures.py causes your
example to raise an exception instead of returning None, and doesn't
seem to break the test suite.

> 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.

That then becomes a pretty minor wart, on par with hash() never
returning -1 (which I came across recently, but only by snooping the
source) - it'll hack it to -2 instead, because -1 is used as an error
signal. In the same way, StopIteration is special-cased as a return
signal (because there needs to be _some_ mechanism for distinguishing
between yield and raise and return; normally, the difference between
"has a value to return" and "has no value to return" is indicated by
raising in the latter case, but now we need even more
distinguishments), it can't actually be raised per se. Since the
exception chains, you can't get confused.

> 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.

Definitely seems like it should be fixed, then; the current behaviour
is that Future.result() raises RuntimeError if you raise
StopIteration, so having await do the same would make sense.

ChrisA

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

How the heck does async/await work in Python 3.5 Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-02-17 22:08 +0000
  Re: How the heck does async/await work in Python 3.5 Steven D'Aprano <steve@pearwood.info> - 2016-02-20 13:36 +1100
    Re: How the heck does async/await work in Python 3.5 Rustom Mody <rustompmody@gmail.com> - 2016-02-19 21:24 -0800
      Re: How the heck does async/await work in Python 3.5 Rustom Mody <rustompmody@gmail.com> - 2016-02-19 21:34 -0800
        Re: How the heck does async/await work in Python 3.5 Paul Rubin <no.email@nospam.invalid> - 2016-02-19 22:44 -0800
          Re: How the heck does async/await work in Python 3.5 Steven D'Aprano <steve@pearwood.info> - 2016-02-21 18:17 +1100
            Re: How the heck does async/await work in Python 3.5 Paul Rubin <no.email@nospam.invalid> - 2016-02-20 23:34 -0800
      Re: How the heck does async/await work in Python 3.5 Ian Kelly <ian.g.kelly@gmail.com> - 2016-02-20 00:48 -0700
      Re: How the heck does async/await work in Python 3.5 Chris Angelico <rosuav@gmail.com> - 2016-02-20 18:57 +1100
      Re: How the heck does async/await work in Python 3.5 Ian Kelly <ian.g.kelly@gmail.com> - 2016-02-20 01:14 -0700
      Re: How the heck does async/await work in Python 3.5 Chris Angelico <rosuav@gmail.com> - 2016-02-20 19:49 +1100
      Re: How the heck does async/await work in Python 3.5 Ian Kelly <ian.g.kelly@gmail.com> - 2016-02-20 02:11 -0700
      Re: How the heck does async/await work in Python 3.5 Ian Kelly <ian.g.kelly@gmail.com> - 2016-02-20 02:21 -0700
    Re: How the heck does async/await work in Python 3.5 Christian Gollwitzer <auriocus@gmx.de> - 2016-02-20 07:53 +0100
      Re: How the heck does async/await work in Python 3.5 "Sven R. Kunze" <srkunze@mail.de> - 2016-02-22 23:16 +0100
      Re: How the heck does async/await work in Python 3.5 Ian Kelly <ian.g.kelly@gmail.com> - 2016-02-22 17:48 -0700
      Re: How the heck does async/await work in Python 3.5 "Sven R. Kunze" <srkunze@mail.de> - 2016-02-23 17:50 +0100
      Re: How the heck does async/await work in Python 3.5 Ian Kelly <ian.g.kelly@gmail.com> - 2016-02-23 10:37 -0700
      Re: How the heck does async/await work in Python 3.5 "Sven R. Kunze" <srkunze@mail.de> - 2016-02-23 20:42 +0100
      Re: How the heck does async/await work in Python 3.5 "Sven R. Kunze" <srkunze@mail.de> - 2016-02-23 22:05 +0100
      Re: [Python-ideas] How the heck does async/await work in Python 3.5 "Joao S. O. Bueno" <jsbueno@python.org.br> - 2016-02-23 18:25 -0300
      Re: [Python-ideas] How the heck does async/await work in Python 3.5 Paul Moore <p.f.moore@gmail.com> - 2016-02-24 09:59 +0000
      Re: [Python-ideas] How the heck does async/await work in Python 3.5 Victor Stinner <victor.stinner@gmail.com> - 2016-02-24 11:01 +0100
      Re: [Python-ideas] How the heck does async/await work in Python 3.5 王珺 <wjun77@gmail.com> - 2016-02-24 18:40 +0800
        Re: [Python-ideas] How the heck does async/await work in Python 3.5 Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2016-02-25 10:00 +1300
          Re: [Python-ideas] How the heck does async/await work in Python 3.5 王珺 <wjun77@gmail.com> - 2016-02-25 08:40 +0800
          Re: [Python-ideas] How the heck does async/await work in Python 3.5 Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2016-02-24 20:37 -0500
      Re: [Python-ideas] How the heck does async/await work in Python 3.5 Tem Pl <rtempl31@gmail.com> - 2016-02-24 06:39 -0800
        Re: [Python-ideas] How the heck does async/await work in Python 3.5 Marko Rauhamaa <marko@pacujo.net> - 2016-02-24 17:23 +0200
          Re: [Python-ideas] How the heck does async/await work in Python 3.5 Ian Kelly <ian.g.kelly@gmail.com> - 2016-02-24 08:41 -0700
            Re: [Python-ideas] How the heck does async/await work in Python 3.5 Marko Rauhamaa <marko@pacujo.net> - 2016-02-24 18:13 +0200
              Re: [Python-ideas] How the heck does async/await work in Python 3.5 Ian Kelly <ian.g.kelly@gmail.com> - 2016-02-24 09:47 -0700

csiph-web