【发布时间】:2019-08-30 11:34:50
【问题描述】:
我只想知道如何从回调中获取数据。
import pika
def callback(channel, method, properties, body):
print(method.get_body())
print(method.get_properties())
channel.basic_ack(delivery_tag=method.delivery_tag)
def on_open(connection):
connection.channel(on_open_callback=on_channel_open)
def on_channel_open(channel):
channel.basic_consume(on_message_callback = callback, queue='q1')
channel.basic_consume(on_message_callback = callback, queue='q2')
credentials = pika.PlainCredentials('user', 'password', erase_on_connect=False)
params = pika.ConnectionParameters("localhost", 5672, '/', credentials)
connection = pika.SelectConnection(parameters=params,
on_open_callback=on_open)
try:
connection.ioloop.start()
except KeyboardInterrupt:
connection.close()
connection.ioloop.start()
回调中两个打印行的输出是:
<class 'pika.spec.Basic.Deliver'>
<Basic.Deliver(['consumer_tag=ctag1.2607da3f5f9f4e5592991a16cc0aca6e', 'delivery_tag=1', 'exchange=gatekeeper', 'redelivered=True', 'routing_key=laa'])>
如何提取“routing_key”?查看源代码让我相信method.get_properties() 会起作用,但它没有。
【问题讨论】:
-
method.routing_key应该是您获取路由密钥的方式。 -
回复作为答案,所以我可以接受这个,传奇的东西兄弟。 (Y)