[py-dev] my first post to the list

Michele Simionato michele.simionato at gmail.com
Tue Apr 26 19:59:11 CEST 2005


Well, after seeing Armin presentation at the ACCU conference
I have decided to keep a close eye on what it is going on
at codespeak.net ;)

He gave a couple of pretty convincing talks. Personally, I am
more  interested in the py library, since it works with current 
Python now, whereas the PyPy project is a more long term
one. So today I spent some time playing with greenlets.

As an exercise, I was trying to implement generators using
greenlets (I know there is an example on site, but I wanted
to do it on my own) and I am a bit confused.

I wanted to write the equivalent of this:

def _gen123():
    yield 1
    yield 2
    yield 3

for i in _gen123():
    print i

I thought this should work:

from py.magic import greenlet

@greenlet
def main():
    while True:
        try:
            print "switching ..."
            print gen123.switch()
        except StopIteration:
            break

@greenlet
def gen123():
    print "gen123 start ..."
    greenlet.getcurrent().parent.switch(1)
    greenlet.getcurrent().parent.switch(2)
    greenlet.getcurrent().parent.switch(3)
    raise StopIteration

main.switch()
print "Done"

However, it does not work as I would expect, the
controls goes immediately back to the toplevel.
Any hint about what I am doing wrong?


              Michele Simionato


More information about the py-dev mailing list