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

Clark C. Evans cce at clarkevans.com
Wed Sep 8 05:29:25 CEST 2004


Josiah Carlson kindly pointed out (off list), that my use of
SuspendIteration violates the standard idiom of exceptions 
terminating the current function. This got past me, beacuse 
I think a generator not as a function, but rather as a shortcut 
to creating iterators.  The offending code is,

|     def NonBlockingResource():
|         yield "one"
|         while True:
|             rand = randint(1,10)
|             if 2 == rand:
|                 break
|             raise SuspendIteration()
|         yield "two"

There are two solutions:
  (a) introduce a new keyword 'suspend'; or,
  (b) don't do that.
  
It is not essential to the proposal that the generator syntax produce
iterators that can SuspendIteration, it is only essential that the
implementation of generators pass-through this exception. Most
non-blocking resources will be low-level components from an async
database or socket library; they can make iterators the old way.

Cheers,

Clark


More information about the Python-Dev mailing list