import time from greenlet import greenlet class Recorder: def __init__(self): self._rec = [] def push(self, data): self._rec.append(data) def pop(self): rst = self._rec[-1] if len(self._rec) != 0 else None return rst def clear(self): del self._rec[:] def __repr__(self): return repr(self._rec) rec = Recorder() def event(i): rec.append(i) def tasklet_test_0(event): """ tasklet test0 """ rec.push(0) event[1].switch(event) def tasklet_test_1(event): """ tasklet test1 """ rec.push(1) event[0].switch(event) def tasklet_switch(): """ tasklet switch """ rec.clear() start_time = time.time() gr0 = greenlet(tasklet_test_0) gr1 = greenlet(tasklet_test_1) event = [gr0, gr1] event[0].switch(event) print repr(rec) print time.time() - start_time def yield_test_0(): rec.push(0) def yield_test_1(): rec.push(1) def yield_test(event): """ list all sequence process in here """ m = yield None while True: if m == "event0": print "receive send %s" %(m) m = yield event[0]() # to event 0 elif m == "event1": print "receive send %s" %(m) m = yield event[1]() # to event 1 else: print "receive send %s" %(m) m = yield None def yield_switch(): """ yield switch """ rec.clear() start_time = time.time() event = [yield_test_0, yield_test_1] iters = yield_test(event) iters.next() # run event and stop iter on it iters.send("event2") iters.send("event1") iters.send("event0") iters.send("stop") print repr(rec) print time.time() - start_time def main(): tasklet_switch() yield_switch() if __name__=='__main__': main()download
ref:
http://sdiehl.github.com/gevent-tutorial/ http://stackoverflow.com/questions/8960747/stackless-in-pypy-and-pypy-greenlet-differences
http://stackoverflow.com/questions/4263059/python-erlang-whats-the-difference-between-twisted-stackless-greenlet-event
沒有留言:
張貼留言