def test0(self): "Simple monitored channel send from main tasklet." import stackless # create players chan = stackless.channel() main = stackless.getmain() # implicit sender receiver = stackless.tasklet(lambda ch:ch.receive()) receiver = receiver(chan) # send a value to a monitored channel chanMon = ChannelMonitor() stackless.set_channel_callback(chanMon) val = 42 chan.send(val) stackless.set_channel_callback(None) # compare sent value with monitored one # found = chanMon.history[0][1].tempval # self.failUnlessEqual(val, found) # FAILS - why? # # fails, because the value is moved from sender to receiver # also, I need to modify channels a little :-) # this one works, because I keep a copy of the value. # # print chanMon.history found = chanMon.history[0][-1] self.failUnlessEqual(val, found)
def test0(self): "Simple monitored channel send from main tasklet." import stackless # create players chan = stackless.channel() main = stackless.getmain() # implicit sender receiver = stackless.tasklet(lambda ch: ch.receive()) receiver = receiver(chan) # send a value to a monitored channel chanMon = ChannelMonitor() stackless.set_channel_callback(chanMon) val = 42 chan.send(val) stackless.set_channel_callback(None) # compare sent value with monitored one # found = chanMon.history[0][1].tempval # self.assertEqual(val, found) # FAILS - why? # # fails, because the value is moved from sender to receiver # also, I need to modify channels a little :-) # this one works, because I keep a copy of the value. # # print chanMon.history found = chanMon.history[0][-1] self.assertEqual(val, found)
def testGetCallback(self): import stackless mon = ChannelMonitor() self.assertIsNone(stackless.get_channel_callback()) old = stackless.set_channel_callback(mon) self.assertIsNone(old) self.assertIs(stackless.get_channel_callback(), mon) old = stackless.set_channel_callback(None) self.assertIs(old, mon) self.assertIsNone(stackless.get_channel_callback())
def main(debug=False): m = mutex() NamedTasklet(f, name="tick")(m) NamedTasklet(f, name="trick")(m) NamedTasklet(f, name="track")(m) if debug: stackless.set_channel_callback(channel_cb) stackless.set_schedule_callback(schedule_cb) stackless.run() stackless.set_channel_callback(None) stackless.set_schedule_callback(None)
def test_channel_callback(self): res = [] cb = [] def callback_function(chan, task, sending, willblock): cb.append((chan, task, sending, willblock)) stackless.set_channel_callback(callback_function) def f(chan): chan.send('hello') val = chan.receive() res.append(val) chan = stackless.channel() task = stackless.tasklet(f)(chan) val = chan.receive() res.append(val) chan.send('world') assert res == ['hello', 'world'] maintask = stackless.getmain() assert cb == [(chan, maintask, 0, 1), (chan, task, 1, 0), (chan, maintask, 1, 1), (chan, task, 0, 0)]
def test_channel_callback(self): res = [] cb = [] def callback_function(chan, task, sending, willblock): cb.append((chan, task, sending, willblock)) stackless.set_channel_callback(callback_function) def f(chan): chan.send('hello') val = chan.receive() res.append(val) chan = stackless.channel() task = stackless.tasklet(f)(chan) val = chan.receive() res.append(val) chan.send('world') assert res == ['hello','world'] maintask = stackless.getmain() assert cb == [ (chan, maintask, 0, 1), (chan, task, 1, 0), (chan, maintask, 1, 1), (chan, task, 0, 0) ]
try: print "recv data: " + channel.receive() except Exception,e: print "recv get cexeption:" + e.message print "recv leave %d" % stackless.getruncount() def send_tasklet(): while True: print "send enter %d" % stackless.getruncount() # channel.send("cao") channel.send_exception(Exception,'dddd') print "send leave %d" % stackless.getruncount() def common_tasklet(): while True: print "common %d" % stackless.getruncount() time.sleep(0.5) stackless.schedule() def channel_cb(channel,tasklet,sending,willblock): print 'I am callback' channel = stackless.channel() stackless.tasklet(recv_tasklet)() stackless.tasklet(send_tasklet)() stackless.tasklet(common_tasklet)() stackless.set_channel_callback(channel_cb) stackless.run()
# Set the "global" trace function for the tasklet next.trace_function = tf # Set the "local" trace function for each frame # This is required, if the tasklet is already running frame = next.frame if frame is current_frame: frame = frame.f_back while frame is not None: frame.f_trace = tf frame = frame.f_back except: traceback.print_exc() finally: sys.settrace(current_tf) if __name__ == "__main__": if len(sys.argv) > 1 and sys.argv[1] == 'hard': stackless.enable_softswitch(False) stackless.set_channel_callback(channel_cb) stackless.set_schedule_callback(schedule_cb) NamedTasklet(task, "tick")() NamedTasklet(task, "trick")() NamedTasklet(task, "track")() stackless.run() stackless.set_channel_callback(None) stackless.set_schedule_callback(None)