[Python-ideas] Revised revised revised PEP on yield-from

Guillaume Chereau charlie137 at gmail.com
Wed Feb 18 03:51:59 CET 2009

On Wed, Feb 18, 2009 at 3:47 AM, Guido van Rossum <guido at python.org> wrote:

> Trying to turn this into a generator like I can do with an ordinary
> file-like object doesn't work:
> def __iter__(self):
>  while True:
>    line = yield from self.readline()
>    if not line: break
>    yield line   ## ???????
> This is because lightweight threads use yield to communicate with the
> scheduler, and they cannot easily also use it to yield successive
> values to their caller. I could imagine some kind of protocol where
> yield always returns a tuple whose first value is a string or token
> indicating what kind of yield it is, e.g. "yield" when it is returning
> the next value from the readline-loop, and "scheduler" when it is
> wanting to talk to the scheduler, but the caller would have to look
> for this and it would become much uglier than just writing out the
> while-loop.
Yes that was the problem I had when I worked with this kind of things.
My solution was to enforce that yielding an other lightweight thread
means that we start it and wait for the result, and yielding anything
else is a return to the caller (I am not very satisfied with this, and
that is why I follow this thread with attention, looking for a better
way to do it)

On a side note, my library didn't use any scheduler, it was simply
turning the generator into a function that can be called with two
callback arguments, and start this function with the send and throw
methods of the calling thread as arguments. I believe that twisted is
using a similar mechanism, but I can't tell for sure for I never had a
deep look at it.

If some people are interested in the code, you can have a look at it
here [0], there are a few use examples at the bottom of the file.

[0] http://git.openmoko.org/?p=tichy.git;a=blob;f=tichy/tasklet.py;


More information about the Python-ideas mailing list