Microthreads without Stackless?

Bengt Richter bokr at oz.net
Sun Sep 19 16:51:52 EDT 2004


On 19 Sep 2004 00:33:56 GMT, bokr at oz.net (Bengt Richter) wrote:
[...]
>(Not tested beyond what you see, it's just an experimental toy ;-)
[...]
----< cotest.py >------------------------------------------------------------
# cotest.py
# coroutine toy with arbitrary parameter passing

def caller(gnak):
    if hasattr(gnak[0],'func_code'): gnak[0] = gnak[0](gnak[1]).next
    return gnak[0]()

class CO(object):
    def __init__(self):
        self.tasks = {}
        self.ready = []
    def add(self, fun):
        self.tasks[fun] = caller.__get__([fun, [(), {}]])
    def call(self, fun, *args, **kw):
        t = self.tasks[fun]
        t.im_self[1][:] = [args, kw]
        self.ready.append(t)
    def run(self):
        while self.ready:
            try: self.ready.pop(0)()
            except StopIteration: pass
-----------------------------------------------------------------------------
should work a little better. I was too hasty ;-/ Probably still buggy, but
at least it's not dependent on the order of adding things, since it defers
initialization of the generators inside caller gnak instance methods. Also
hope I got rid of circular reference to gnak. (Still not tested worth worth
a dang, no time. ;-)

Regards,
Bengt Richter



More information about the Python-list mailing list