代码之家  ›  专栏  ›  技术社区  ›  Géry Ogam

是否可以在Python中使用RabbitMQ直接回复功能和Pika生成器使用者一起使用?

  •  0
  • Géry Ogam  · 技术社区  · 7 年前

    我想使用 direct reply-to RabbitMQ的特性 Pika Python中的客户端库。它与一个 基本的 消费者。但它引发了以下异常 发电机

    pika.exceptions.ChannelClosedByBroker:(406,“前提条件失败-快速回复消费者不存在”)

    有没有一种方法可以对生成器使用者使用直接回复功能?

    使用基本使用者的客户机代码示例(它起作用):

    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()
    

    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)
    

    环境。 Windows 10、RabbitMQ 3.7.13、CPython 3.7.3、Pika 1.0.1。

    注意。 呼叫 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()
    
    0 回复  |  直到 7 年前
        1
  •  1
  •   Géry Ogam    7 年前

    正如卢克·巴克肯建议的那样 here

    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)