本文整理汇总了Python中pyon.net.endpoint.Subscriber.prepare_listener方法的典型用法代码示例。如果您正苦于以下问题:Python Subscriber.prepare_listener方法的具体用法?Python Subscriber.prepare_listener怎么用?Python Subscriber.prepare_listener使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyon.net.endpoint.Subscriber
的用法示例。
在下文中一共展示了Subscriber.prepare_listener方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_consume_one_message_at_a_time
# 需要导入模块: from pyon.net.endpoint import Subscriber [as 别名]
# 或者: from pyon.net.endpoint.Subscriber import prepare_listener [as 别名]
def test_consume_one_message_at_a_time(self):
# see also pyon.net.test.test_channel:TestChannelInt.test_consume_one_message_at_a_time
pub3 = Publisher(to_name=(self.container.ex_manager.default_xs.exchange, 'routed.3'))
pub5 = Publisher(to_name=(self.container.ex_manager.default_xs.exchange, 'routed.5'))
#
# SETUP COMPLETE, BEGIN TESTING OF EXCHANGE OBJECTS
#
xq = self.container.ex_manager.create_xn_queue('random_queue')
self.addCleanup(xq.delete)
# recv'd messages from the subscriber
self.recv_queue = Queue()
sub = Subscriber(from_name=xq, callback=lambda m,h: self.recv_queue.put((m, h)))
sub.prepare_listener()
# publish 10 messages - we're not bound yet, so they'll just dissapear
for x in xrange(10):
pub3.publish("3,%s" % str(x))
# no messages yet
self.assertFalse(sub.get_one_msg(timeout=0))
# now, we'll bind the xq
xq.bind('routed.3')
# even tho we are consuming, there are no messages - the previously published ones all dissapeared
self.assertFalse(sub.get_one_msg(timeout=0))
# publish those messages again
for x in xrange(10):
pub3.publish("3,%s" % str(x))
# NOW we have messages!
for x in xrange(10):
self.assertTrue(sub.get_one_msg(timeout=0))
m,h = self.recv_queue.get(timeout=0)
self.assertEquals(m, "3,%s" % str(x))
# we've cleared it all
self.assertFalse(sub.get_one_msg(timeout=0))
# bind a wildcard and publish on both
xq.bind('routed.*')
for x in xrange(10):
time.sleep(0.3)
pub3.publish("3,%s" % str(x))
time.sleep(0.3)
pub5.publish("5,%s" % str(x))
# should get all 20, interleaved
for x in xrange(10):
self.assertTrue(sub.get_one_msg(timeout=0))
m, h = self.recv_queue.get(timeout=0)
self.assertEquals(m, "3,%s" % str(x))
self.assertTrue(sub.get_one_msg(timeout=0))
m, h = self.recv_queue.get(timeout=0)
self.assertEquals(m, "5,%s" % str(x))
# add 5 binding, remove all other bindings
xq.bind('routed.5')
xq.unbind('routed.3')
xq.unbind('routed.*')
# try publishing to 3, shouldn't arrive anymore
pub3.publish("3")
self.assertFalse(sub.get_one_msg(timeout=0))
# let's turn off the consumer and let things build up a bit
sub._chan.stop_consume()
for x in xrange(10):
pub5.publish("5,%s" % str(x))
# 10 messages in the queue, no consumers
self.assertTupleEqual((10, 0), sub._chan.get_stats())
# drain queue
sub._chan.start_consume()
time.sleep(1) # yield to allow delivery
for x in xrange(10):
self.assertTrue(sub.get_one_msg(timeout=0))
self.recv_queue.get(timeout=0)
sub.close()