> pika.exceptions.ChannelClosedByBroker: (406, 'PRECONDITION_FAILED - reply consumer already set')
**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)
Sample server code for both consumers:
import pika
def handle(channel, method, properties, body):
message = body.decode()
print("received:", message)
message = "world"
channel.basic_publish(
exchange="", routing_key=properties.reply_to, body=message.encode())
print("sent:", message)
channel.basic_ack(method.delivery_tag)
connection = pika.BlockingConnection()
channel = connection.channel()
with connection, channel:
for (method, properties, body) in channel.consume(queue="test"):
handle(channel, method, properties, body)
Environment: Windows 10, RabbitMQ 3.7.13, CPython 3.7.3, Pika 1.0.1.