def example_2(): print """EXAMPLE 2 1) Open a new channel. 2) Register a new handler to that channel. 3) Register a second handler to the same channel, but with a different handling method. 4) Publish to that channel. 5) Close the channel entirely. 6) Publish to same channel. (No output from this one) OUTPUT:""" def evt_handler1(evt): print "evt_handler1:" print " __channel:", evt.get_channel() print " name:", evt.name def evt_handler2(evt): print "evt_handler2:" print " __channel:", evt.get_channel() print " name:", evt.name print " int_arg:", evt.int_arg pyback.open_channel('example_2_channel') pyback.subscribe('example_2_channel', evt_handler1) pyback.subscribe('example_2_channel', evt_handler2) # push from anywhere that imports pyback pyback.publish('example_2_channel', name="example_2_evt", int_arg=10) # close the channel entirely. # this automatically unsubscribes all event handlers on this channel. pyback.close_channel('example_2_channel') # this will cause no callback pyback.publish('example_2_channel', name="example_2_evt_fail", int_arg=20) print "-" * 50
def test_one_publisher_two_subscribers(self): import pyback self.called1 = 0 self.called2 = 0 def callback1(evt): self.called1 += 1 def callback2(evt): self.called2 += 1 pyback.subscribe('testchannel', callback1) pyback.subscribe('testchannel', callback2) pyback.publish('testchannel') self.assertEqual(self.called1, 1) self.assertEqual(self.called2, 1) pyback.unsubscribe('testchannel', callback2) pyback.publish('testchannel') self.assertEqual(self.called1, 2) self.assertEqual(self.called2, 1)
def example_4(): print """EXAMPLE 4 In this example, we register a class method as a handler for evt. Not too difficult, really. OUTPUT:""" class Dummy(object): def __init__(self, channel_key): # the object subscribes itself to designated # channel on __init__. This could also be done # from outside the object scope desired. pyback.subscribe(channel_key, self.class_evt_handler) def class_evt_handler(self, evt): print "__channel:", evt.get_channel() print "evt name:", evt.name dum = Dummy('example_4_channel') # publish an event pyback.publish('example_4_channel', name='instantiated_handler_evt') # unregister from outside the object scope pyback.unsubscribe('example_4_channel', dum.class_evt_handler) # publish again, this one will be ignored, since handler # was unsubscribed pyback.publish('example_4_channel', name='instantiated_handler_evt2')
def test_close_channel(self): import pyback self.called = 0 def callback(evt): self.called += 1 pyback.subscribe('testchannel', callback) pyback.publish('testchannel') self.assertEqual(self.called, 1) pyback.close_channel('testchannel') pyback.publish('testchannel') self.assertEqual(self.called, 1) # NOT 2
def test_two_publishers_one_subscriber(self): import pyback self.called = 0 def callback(evt): self.called += 1 pyback.subscribe('testchannel', callback) pyback.publish('testchannel') self.assertEqual(self.called, 1) pyback.publish('testchannel') self.assertEqual(self.called, 2) pyback.unsubscribe('testchannel', callback)
def test_object_callback(self): import pyback class Dummy(object): def __init__(self): self.called = 0 def object_callback(self, evt): self.called += 1 dum = Dummy() pyback.subscribe('testchannel', dum.object_callback) pyback.publish('testchannel', thing='blah') self.assertEqual(dum.called, 1) pyback.unsubscribe('testchannel', dum.object_callback)
def consumer_method(consumer_id, queue, channel_key): while True: try: string = queue.get_nowait() except Queue.Empty, e: # queue is now empty break # perform processing print "{0} thread processed:".format(consumer_id), string # issue an event over the passed channel pyback.publish(channel_key, task_data=string, consumer_id=consumer_id) # task processing. queue.task_done()
def test_static_sub_pub(self): import pyback self.called = 0 def callback(evt): self.called += 1 self.assertIsInstance(evt, pyback.Evt) self.assertEqual(evt.testval, 42) pyback.subscribe('testchannel', callback) pyback.publish('testchannel', testval=42) self.assertEqual(self.called, 1) # remove it pyback.unsubscribe('testchannel', callback) pyback.publish('testchannel', testval=42) self.assertEqual(self.called, 1) # NOT 2
def example_1(): print """EXAMPLE 1 1) Register a new handler. 2) Publish to channel. 3) Unsubscribe handler from channel. OUTPUT:""" def evt_handler(evt): # do something with evt here. # For sake of argument (haha, get it?), we will # print a custom passed kwarg called "name". print "__channel:", evt.get_channel() print "name:", evt.name pyback.subscribe('example_1_channel', evt_handler) # now this could be launched from anywhere that # imports the pyback module: pyback.publish('example_1_channel', name="example_1_event") pyback.unsubscribe('example_1_channel', evt_handler) print "-" * 50