作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我只是想知道如何从回调中获取数据。
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()
会起作用,但事实并非如此。
最佳答案
尽管记录很少,callback
函数将使用 4 个参数调用:
Method
实例(在本例中为 Deliver
实例)BasicProperties
实例字节
)Deliver
实例将具有一个名为 routing_key
的属性。所以你的函数可能如下所示:
def callback(channel, method, properties, body):
print(method.get_body())
print(method.get_properties())
print(method.routing_key)
channel.basic_ack(delivery_tag=method.delivery_tag)
PS。调用回调的参数与
here 中描述的相同。 ,它们
实际记录在其中。
关于python - 如何从回调中获取信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57725991/
我是一名优秀的程序员,十分优秀!