[Stackless] Explicit yield/break to Preemptive Stackless Loop
Jeff Senn
senn at maya.com
Wed Nov 1 21:14:08 CET 2006
Hi Brian-
On Nov 1, 2006, at 2:10 PM, Brian Hardie wrote:
>
> stackless.schedule() and stackless.schedule_remove() do a fine job of
> yielding to the next tasklet, but it seems, in my situation of having
> only a single tasklet, that it does not cause a breakout of the
> 'Stackless loop'.
I might question why you want to break out of the loop - rather
than just having another tasklet do that work... (see second example
below).
There is no "yield" or "break" as you describe since, for most
examples, there are
other tasklets wanting to do work. You need to basically remove
everything from the runnables to get stackless.run() to return
before a quantum.
...but assuming that's what you want, for your case (this obviously
won't
work if there are more tasklets...) you can just trivially use a channel
to take your tasklet off the run queue. Example below.
(the only bit of subtlety being that you don't want to call
"channel.send"
from outside a tasket...)
-Jas
------
import stackless
def stuff(c):
print "start"
for i in xrange(0,1000):
if i == 500 or i == 600:
print "pause",i
c.receive() #block myself
print "continue"
print "done"
def restart(c): c.send(None) #use a task to restart other task
c = stackless.channel()
x = stackless.tasklet(stuff)(c)
while stackless.getruncount() > 1:
t = stackless.run(1000)
print "DO WHATEVER HERE"
if c.balance < 0: stackless.tasklet(restart)(c)
-------
## second example using cooperating processes
## and not even bothering with channels
import stackless
def main():
print "start"
for i in xrange(0,1000):
if i == 500 or i == 600:
print "pause"
stackless.schedule() #will make other guy run
print "continue"
print "done"
def other():
while stackless.getruncount() > 1:
print "periodic check stuff"
stackless.schedule() #will make first guy run again
stackless.tasklet(main)()
stackless.tasklet(other)()
while stackless.getruncount() > 1:
t = stackless.run(1000)
if t is not None: t.run() #pick up where we left off
_______________________________________________
Stackless mailing list
Stackless at stackless.com
http://www.stackless.com/mailman/listinfo/stackless
More information about the Stackless
mailing list