[Stackless] Coroutines

Christian Tismer tismer at stackless.com
Thu May 27 17:20:54 CEST 2004


Jimmie Houchin wrote:

> Hello,
> 
> I am trying to learn coroutines.
> 
> Is there any tutorial or documentation on how to use coroutines with 
> Stackless.

There will be more direct support soon, but here is
the simple way:
take two tasklets, give them a channel, and let them talk.

def coro1(ch):
   while somecondition:
     # do some stuff
     # talk to the other one
     ch.send(result)
     # ...

def coro2(ch):
   while someothercondition:
     # do some other stuff
     # get info of other coroutine
     info = ch.receive()
     # ...

#Ok, now run that stuff:

from stackless import *

ch = channel()
t1 = tasklet(coro1)(ch)
t2 = tasklet(coro2)(ch)

# run waits until there is no tasklet to run anymore

run()

-- 
Christian Tismer             :^)   <mailto:tismer at stackless.com>
Mission Impossible 5oftware  :     Have a break! Take a ride on Python's
Johannes-Niemeyer-Weg 9a     :    *Starship* http://starship.python.net/
14109 Berlin                 :     PGP key -> http://wwwkeys.pgp.net/
work +49 30 89 09 53 34  home +49 30 802 86 56  mobile +49 173 24 18 776
PGP 0x57F3BF04       9064 F4E1 D754 C2FF 1619  305B C09C 5A3B 57F3 BF04
      whom do you want to sponsor today?   http://www.stackless.com/


_______________________________________________
Stackless mailing list
Stackless at stackless.com
http://www.stackless.com/mailman/listinfo/stackless



More information about the Stackless mailing list