[Python-Dev] PEP 492: What is the real goal?

Guido van Rossum guido at python.org
Thu Apr 30 19:55:08 CEST 2015


On Thu, Apr 30, 2015 at 10:24 AM, Jim J. Jewett <jimjjewett at gmail.com>
wrote:

> I suspect the real problem is that the PEP is really only concerned
> with a very specific subtype of coroutine, and these don't quite fit.
>

That's correct. The PEP is concerned with the existing notion of coroutines
in Python, which was first introduced by PEP 342: Coroutines via Enhanced
Generators. The Wikipedia definition of coroutine (which IIRC is due to
Knuth) is quite different and nobody who actually uses the coding style
introduced by PEP 342 should mistake one for the other.

 This same notion of "Pythonic" (so to speak) coroutines was refined by PEP
380, which introduced yield from. It was then *used* in PEP 3156 (the
asyncio package) for the specific purpose of standardizing a way to do I/O
multiplexing using an event loop.

The basic premise of using coroutines with the asyncio package is that most
of the time you can write *almost* sequential code as long as you insert
"yield from" in front of all blocking operations (and as long as you use
blocking operations that are implemented by or on top of the asyncio
package). This makes the code easier to follow than code written
"traditional" event-loop-based I/O multiplexing (which is heavy on
callbacks, or callback-like abstractions like Twisted's Deferred).

However, heavy users of the asyncio package (like Yury) discovered some
common patterns when using coroutines that were awkward. In particular,
"yield from" is quite a mouthful, the coroutine version of a for-loop is
awkward, and a with-statement can't have a blocking operation in __exit__
(because there's no explicit yield opcode). PEP 492 proposes a quite simple
and elegant solution for these issues. Most of the technical discussion
about the PEP is on getting the details right so that users won't have to
worry about them, and can instead just continue to write *almost*
sequential code when using the asyncio package (or some other framework
that offers an event loop integrated with coroutines).

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


More information about the Python-Dev mailing list