Exemplo n.º 1
0
    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)
Exemplo n.º 2
0
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
Exemplo n.º 3
0
    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)
Exemplo n.º 4
0
    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
Exemplo n.º 5
0
    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
Exemplo n.º 6
0
    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)
Exemplo n.º 7
0
    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)
Exemplo n.º 8
0
    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)
Exemplo n.º 9
0
    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)
Exemplo n.º 10
0
    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
Exemplo n.º 11
0
    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
Exemplo n.º 12
0
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
Exemplo n.º 13
0
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
Exemplo n.º 14
0
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
Exemplo n.º 15
0
	def evt_handler(evt):
		# alert user to task processing
		print "Evt:"
		print "  task_data:", evt.task_data
		print "  consumer_id:", evt.consumer_id

	queue = Queue.Queue()
	queue.put('task1')
	queue.put('task2')
	queue.put('task3')
	queue.put('task4')
	queue.put('task5')
	queue.put('task6')

	channel_key = 'consumer_channel'
	pyback.subscribe(channel_key, evt_handler)

	# launch consumer
	# NOTE: thread2 is commented out because it makes use of
	# stdout, which is NON-THREAD-SAFE, causing output to be
	# out of order. To use stdout from within event handlers,
	# it will be necessary to use a thread-safe print method.

	thread = threading.Thread(target=consumer_method, args=('consumerID#1', queue, channel_key))
	#thread2 = threading.Thread(target=consumer_method, args=('consumerID#2', queue, channel_key))
	thread.start()
	#thread2.start()
	thread.join()
	#thread2.join()

	print "-" * 50
Exemplo n.º 16
0
    def evt_handler(evt):
        # alert user to task processing
        print "Evt:"
        print "  task_data:", evt.task_data
        print "  consumer_id:", evt.consumer_id

    queue = Queue.Queue()
    queue.put('task1')
    queue.put('task2')
    queue.put('task3')
    queue.put('task4')
    queue.put('task5')
    queue.put('task6')

    channel_key = 'consumer_channel'
    pyback.subscribe(channel_key, evt_handler)

    # launch consumer
    # NOTE: thread2 is commented out because it makes use of
    # stdout, which is NON-THREAD-SAFE, causing output to be
    # out of order. To use stdout from within event handlers,
    # it will be necessary to use a thread-safe print method.

    thread = threading.Thread(target=consumer_method,
                              args=('consumerID#1', queue, channel_key))
    #thread2 = threading.Thread(target=consumer_method, args=('consumerID#2', queue, channel_key))
    thread.start()
    #thread2.start()
    thread.join()
    #thread2.join()
Exemplo n.º 17
0
 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)
Exemplo n.º 18
0
		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)