[Python-Dev] PEP 334 - Simple Coroutines via SuspendIteration

James Y Knight foom at fuhm.net
Wed Sep 8 20:08:17 CEST 2004

On Sep 7, 2004, at 9:48 PM, Clark C. Evans wrote:

> I've packaged up the idea of a coroutine facility using iterators and 
> an
> exception, SuspendIteration.

Very interesting.

> This proposal assumes that a corresponding iterator written using
> this class-based method is possible for existing generators.  The
> challenge seems to be the identification of distinct states within
> the generator where suspension could occur.

That is basically impossible. Essentially *every* operation could 
possibly raise SuspendIteration, because essentially every operation 
can call an arbitrary python function, and python functions can raise 
any exception they want. I think you could still make the proposal work 
in CPython: if I understand its internals properly, it doesn't need to 
do a transformation to a class iterator, it simply suspends the frame 
wherever it is. Thus, being able to suspend at any point in the 
function would not cause an undue performance degradation.

However, I think it is a deal-breaker for JPython. From the generator 
PEP: "It's also believed that efficient implementation in Jython 
requires that the compiler be able to determine potential suspension 
points at compile-time, and a new keyword makes that easy." If this 
quote is right about the implementation of Jython (and it seems likely, 
given the JVM), your proposal makes it impossible to implement 
generators in Jython.

Given that the advantage claimed for this proposal over stackless is 
that it can be implemented in non-CPython runtimes, I think it still 
needs some reworking.


More information about the Python-Dev mailing list