[Stackless] Explicit yield/break to Preemptive Stackless Loop

Jeff Senn senn at maya.com
Fri Nov 3 17:19:31 CET 2006


On Nov 2, 2006, at 8:10 PM, Brian Hardie wrote:

> I think I should describe my situation a bit more.
...snipped for brevity...

Brian - I'm at a loss to explain the channel behavior your
example exhibits at the moment... let me just suggest something
simpler (since you want to pickle anyway....)  if I get a chance
I'll look into it more later.

Instead of using a channel just remove the tasklet from runnables.
(There is a trick to doing this -- since you can't just remove  
yourself).

Your example re-worked:
-----------------------

from threading import Thread
import stackless

def remove_task(x): x.remove()

class myThread(Thread):
	def __init__(self, i):
		self.i = i
		Thread.__init__(self)

	def run(self):
		if self.i:
			self.stackloop()
		else:
			self.i_count()
			
	def stackloop(self):
		self.ch = stackless.channel()
		t1 = stackless.tasklet(self.count)()
		while stackless.getruncount() > 1:
        			t = stackless.run(10)
			if t is None:
				t = t1 #put it back
				# or pickle...whatever
        	 		raw_input("ENTER TO CONTINUE")
			t.insert()
			
	def sendWakeup(self):
		self.ch.send()

	def count(self):
		i = 0
		while 1:
			print i
			i += 1
			t = stackless.getcurrent()
			stackless.tasklet(remove_task)(t)
			stackless.schedule()
		
	def i_count(self):
		i=0
		while 1:
			i+=1
			if i% 5000000 == 0:
			  print "still running..."


non_task = myThread(False)
task = myThread(True)

non_task.start()
task.start()




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



More information about the Stackless mailing list