[Python-Dev] PEP 479 and asyncio

Guido van Rossum guido at python.org
Thu Nov 27 20:06:46 CET 2014


On Thu, Nov 27, 2014 at 10:08 AM, Victor Stinner <victor.stinner at gmail.com>
wrote:

> I'm trying to follow the discussion about the PEP 479 (Change
> StopIteration handling inside generators), but it's hard to read all
> messages. I'm concerned by trollius and asyncio which heavily rely on
> StopIteration.
>
> Trollius currently supports running asyncio coroutines: a trollius
> coroutine can executes an asyncio coroutine, and and asyncio coroutine
> can execute a trollius coroutine.
>
> I modified the Return class of Trollius to not inherit from
> StopIteration. All trollius tests pass on Python 3.3 except on one
> (which makes me happy, the test suite is wide enough to detect bugs
> ;-)): test_trollius_in_asyncio.
>
> This specific test executes an asyncio which executes a trollius coroutine.
>
> https://bitbucket.org/enovance/trollius/src/873d21ac0badec36835ed24d13e2aeda24f2dc64/tests/test_asyncio.py?at=trollius#cl-60
>
> The problem is that an asyncio coroutine cannot execute a Trollius
> coroutine anymore: "yield from coro" raises a Return exception instead
> of simply "stopping" the generator and return the result (value passed
> to Return).
>
> I don't see how an asyncio coroutine calling "yield from
> trollius_coroutine" can handle the Return exception if it doesn't
> inherit from StopIteration. It means that I have to drop this feature
> in Python 3.5 (or later when the PEP 479 becomes effective)?
>
> I'm talking about the current behaviour of Python 3.3, I didn't try
> the PEP 479 (I don't know if an exception exists).
>

The issue here is that asyncio only interprets StopIteration as returning
from the generator (with a possible value), while a Trollius coroutine must
use "raise Return(<value>)" to specify a return value; this works as long
as Return is a subclass of StopIteration, but PEP 479 will break this by
replacing the StopIteration with RuntimeError.

It's an interesting puzzle.

The only way out I can think of is to have asyncio special-case the Return
exception -- we could do that by defining a new exception (e.g.
AlternateReturn) in asyncio that gets treated the same way as
StopIteration, so that Trollius can inherit from AlternateReturn (if it
exists).

What do you think?

-- 
--Guido van Rossum (python.org/~guido)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20141127/ce61e68f/attachment.html>


More information about the Python-Dev mailing list