Hi all.
I found cooperative multi-threading(only one thread runs at once,
explicit thread switching) is useful for writing some simulators.
With it, I'm able to be free from annoying mutual exclusion, and make
results deterministic.
For this purpose, and inspired by Ruby(1.9) fiber, I wrote my own
version of fiber in Python.
It just works, but using native Python threads for non-preemptive
threading is not cost-effective. Python has generator instead but it
seemed to be very restricted for general scripting. I wish I could
write nested (generator) functions easily at least.
Is there any plan of implementing real (lightweight) fiber in Python?
------------------------------------------------------------
import threading
class Fiber(threading .Thread):
def __init__(self):
threading.Threa d.__init__(self )
self.semaphore_ running = threading.Semap hore(0)
self.semaphore_ finish = None
self.val = None
self.setDaemon( True)
self.start()
self.start = self.start_fibe r
def start_fiber(sel f):
self.semaphore_ finish = threading.Semap hore(0)
self.semaphore_ running.release ()
self.semaphore_ finish.acquire( )
def run(self): # override
self.semaphore_ running.acquire ()
self.main()
if self.semaphore_ finish is not None:
self.semaphore_ finish.release( )
def switchto(self, fiber, val=None):
fiber.val = val
fiber.semaphore _running.releas e()
self.semaphore_ running.acquire ()
return self.val
def main(self): # should be overridden
pass
class F1(Fiber):
def main(self):
print "f1 start"
self.switchto(f 2)
print "f1 foo"
v = self.switchto(f 2)
print "f1 v=%s world" % v
self.switchto(f 2, "OK")
print "f1 end"
class F2(Fiber):
def main(self):
print "f2 start"
self.switchto(f 1)
print "f2 bar"
result = self.switchto(f 1, "Hello, ")
print "f2 result=%s" % result
print "f2 end"
self.switchto(f 1)
f1 = F1()
f2 = F2()
print "start"
f1.start()
print "end"
-- kayama
Comment