fiber(cooperative multi-threading)

Akihiro KAYAMA kayama at st.rim.or.jp
Sat Dec 22 07:10:39 EST 2007


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.Thread.__init__(self)

        self.semaphore_running = threading.Semaphore(0)
        self.semaphore_finish = None
        self.val = None

        self.setDaemon(True)
        self.start()
        self.start = self.start_fiber

    def start_fiber(self):
        self.semaphore_finish = threading.Semaphore(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.release()
        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(f2)
        print "f1 foo"
        v = self.switchto(f2)
        print "f1 v=%s world" % v
        self.switchto(f2, "OK")
        print "f1 end"

class F2(Fiber):
    def main(self):
        print "f2 start"
        self.switchto(f1)
        print "f2 bar"
        result = self.switchto(f1, "Hello, ")
        print "f2 result=%s" % result
        print "f2 end"
        self.switchto(f1)

f1 = F1()
f2 = F2()

print "start"
f1.start()
print "end"

-- kayama



More information about the Python-list mailing list