【问题标题】:re-queue all messages from one queue to another with pika使用 pika 将所有消息从一个队列重新排队到另一个队列
【发布时间】:2014-09-04 18:59:25
【问题描述】:

我需要遍历回调队列中的所有消息,然后关闭回调。一旦队列为空,我需要它停止消费。 所以我将消息从一个队列写入另一个队列。

creds = pika.PlainCredentials(app.config['mq.user'], app.config['mq.pass']) connection = pika.BlockingConnection(pika.ConnectionParameters( host=app.config['mq.host'], credentials=creds)) connection2 = pika.BlockingConnection(pika.ConnectionParameters( host=app.config['mq.host'], credentials=creds))

    channel = connection.channel()
    channel2 = connection2.channel()

Def requeue_callback(ch, method, properties, body):
try:
    msg = json.loads(body)
    ch2.basic_publish(exchange='',
                      routing_key=base_queue+'.request',
                      body = msg['orig_msg'])
finally:
    ch.basic_ack(delivery_tag = method.delivery_tag)

channel.basic_consume(requeue_callback,
                  queue=base_queue+'.error')

channel.start_consuming()

*另外,我可以找到队列中的消息数量,然后使用该特定数量。在那种情况下,我将如何重新排队特定号码。

【问题讨论】:

    标签: python queue rabbitmq pika


    【解决方案1】:

    在您的回调函数中,您可以使用passive=True 声明队列,然后使用结果来获取消息数。

    例如:

    >>> ch = rabbit.connection.channel()
    >>> method = ch.queue_declare('sandbox', passive=True)
    >>> method.method.message_count
    23
    >>> 
    

    然后检查这个数是否为0。

    【讨论】:

      猜你喜欢
      • 2014-04-13
      • 2012-02-06
      • 1970-01-01
      • 1970-01-01
      • 2020-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-06
      相关资源
      最近更新 更多