[英]Is it possible to use RabbitMQ direct reply-to feature with a Pika generator consumer in Python?
I would like to use the direct reply-to feature of RabbitMQ with the Pika client library in Python.我想将 RabbitMQ 的直接回复功能与 Python 中的Pika客户端库一起使用。 It works with a basic consumer.
它适用于基本消费者。 But it raises the following exception with a generator consumer:
但它引发了以下异常与生成器消费者:
pika.exceptions.ChannelClosedByBroker: (406, 'PRECONDITION_FAILED - fast reply consumer does not exist')
pika.exceptions.ChannelClosedByBroker: (406, 'PRECONDITION_FAILED - 快速回复消费者不存在')
Is there a way to use the direct reply-to feature with a generator consumer?有没有办法对生成器使用者使用直接回复功能?
Sample client code using a basic consumer (it works):使用基本消费者的示例客户端代码(它有效):
import pika
def handle(channel, method, properties, body):
message = body.decode()
print("received:", message)
connection = pika.BlockingConnection()
channel = connection.channel()
with connection, channel:
message = "hello"
channel.basic_consume(queue="amq.rabbitmq.reply-to",
on_message_callback=handle, auto_ack=True)
channel.basic_publish(
exchange="", routing_key="test", body=message.encode(),
properties=pika.BasicProperties(reply_to="amq.rabbitmq.reply-to"))
print("sent:", message)
channel.start_consuming()
Sample client code using a generator consumer (it raises the exception):使用生成器使用者的示例客户端代码(它引发异常):
import pika
def handle(channel, method, properties, body):
message = body.decode()
print("received:", message)
connection = pika.BlockingConnection()
channel = connection.channel()
with connection, channel:
message = "hello"
channel.basic_publish(
exchange="", routing_key="test", body=message.encode(),
properties=pika.BasicProperties(reply_to="amq.rabbitmq.reply-to"))
print("sent:", message)
for (method, properties, body) in channel.consume(
queue="amq.rabbitmq.reply-to", auto_ack=True):
handle(channel, method, properties, body)
Environment.环境。 — Windows 10, RabbitMQ 3.7.13, CPython 3.7.3, Pika 1.0.1.
— Windows 10、RabbitMQ 3.7.13、CPython 3.7.3、Pika 1.0.1。
Note.笔记。 — Calling the
basic_consume
method after the basic_publish
method in the sample client code using a basic consumer raises the same exception as when using a generator consumer: -调用
basic_consume
后方法basic_publish
使用碱性消费者在样本客户机代码方法引发相同的异常使用发电机消费者时为:
import pika
def handle(channel, method, properties, body):
message = body.decode()
print("received:", message)
connection = pika.BlockingConnection()
channel = connection.channel()
with connection, channel:
message = "hello"
channel.basic_publish(
exchange="", routing_key="test", body=message.encode(),
properties=pika.BasicProperties(reply_to="amq.rabbitmq.reply-to"))
print("sent:", message)
channel.basic_consume(queue="amq.rabbitmq.reply-to",
on_message_callback=handle, auto_ack=True)
channel.start_consuming()
As suggested by Luke Bakken here , this does the trick: 正如Luke Bakken 在这里建议的那样,它可以解决问题:
import pika
def handle(channel, method, properties, body):
message = body.decode()
print("received:", message)
connection = pika.BlockingConnection()
channel = connection.channel()
with connection, channel:
message = "hello"
next(channel.consume(queue="amq.rabbitmq.reply-to", auto_ack=True,
inactivity_timeout=0.1))
channel.basic_publish(
exchange="", routing_key="test", body=message.encode(),
properties=pika.BasicProperties(reply_to="amq.rabbitmq.reply-to"))
print("sent:", message)
for (method, properties, body) in channel.consume(
queue="amq.rabbitmq.reply-to", auto_ack=True):
handle(channel, method, properties, body)
i would recommend the example given with the pika documentation.我会推荐 pika 文档中给出的示例。
it did work for me too.它也对我有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.