[py-dev] my first post to the list
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
On 4/26/05, Michele Simionato <michele.simionato@gmail.com> wrote:
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.
For the record, I have solved the confusion on my own. I misunderstood what the parent greenlet is. I had the wrong idea that the parent greenlet was the calling greenlet, whereas it is just the greenlet where the child greenlet is *defined*, not called. So my example works just by setting the right parent by hand: gen123.parent = main I see that the same trick is used in test_generators.py. Still thinking to practical applications of the idea, but I believe I understand the basics now. Michele
Hi all, On Tue, Apr 26, 2005 at 01:59:11PM -0400, Michele Simionato wrote:
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 ;)
For the records, as Michele asked me, I've put the greenlet example I gave in the lightning talk into the greenlet documentation: http://codespeak.net/py/current/doc/greenlet.html#example Armin
On 4/28/05, Armin Rigo <arigo@tunes.org> wrote:
For the records, as Michele asked me, I've put the greenlet example I gave in the lightning talk into the greenlet documentation:
I think the example could be improved a bit. We have def process_commands(*args): while True: line = '' while not line.endswith('\n'): line += read_next_char() if line == 'quit\n': print "are you sure?" if read_next_char() != 'y': continue # ignore the command process_command(line) but the *args are ignored and 'process_command' looks too similar to process_commands; also the continue line looks strange. Why not just def show_entered_commands(): while True: line = '' while not line.endswith('\n'): line += read_next_char() print line if line == 'quit\n': print "are you sure?" if read_next_char() == 'y': break ? Anyway, this is a pretty cool example of what you can do with greenlets. Michele
BTW, I was continuing my experiments with greenlets and I seems that I cannot pickle a greenlet, I cannot copy it reliably, and I cannot restart it. IOW, apparently there is no way to save an execution context and restart it later. Am I correct? These limitations should be documented. Michele Simionato
participants (2)
-
Armin Rigo -
Michele Simionato