是否有可能从鼠兔的每个队列消费?

Auh*_*Auh 1 python rabbitmq pika

我正在尝试设置一个程序,它将从RabbitMQ中的每个队列中消耗,并根据某些消息运行某些脚本.不幸的是,当添加消费者时,如果它遇到单个错误(即没有找到超时或队列),则整个通道都已死亡.此外,队列来来去去,所以它必须经常刷新队列列表.这甚至可能吗?到目前为止,这是我的代码.

import pika
import requests
import sys

try:
    host = sys.argv[1]
except:
    host = "localhost"

def get_queues(host="localhost", port=15672, user="guest", passwd="guest", virtual_host=None):
    url = 'http://%s:%s/api/queues/%s' % (host, port, virtual_host or '')
    response = requests.get(url, auth=(user, passwd))
    return response.json()

queues = get_queues(host)

def get_on_message(queue):
    def on_message(channel, method_frame, header_frame, body):
        print("message from", queue)
        channel.basic_ack(delivery_tag=method_frame.delivery_tag)
    return on_message

connection = pika.BlockingConnection(pika.ConnectionParameters(host))
channel = connection.channel()

for queue in queues:
    print(channel.is_open)
    try:
        channel.basic_consume(get_on_message(queue["name"]), queue["name"])
        print("queue added",queue["name"])
    except Exception as e:
        print("queue failed",queue["name"])
        sys.exit()

try:
    channel.start_consuming()
except KeyboardInterrupt:
    channel.stop_consuming()
connection.close()
Run Code Online (Sandbox Code Playgroud)

有没有正确的方法来做到这一点,或者这样做是不是正确的?

can*_*Now 6

可以使用任何语言的每个队列进行消费.这也是错误的,如果这是必需的,那么整个设计/设置应该重新思考.

评论后编辑:

基本上,您需要获取所有现有队列的名称,这些队列可以通过其余的api以编程方式完成(甚至可能通过调用rabbitmqctl并解析输出).获得队列的名称后,您可以直接使用它们,如教程所述.

再一次,我不认为这是正确的方法,也许你应该考虑使用主题交换 - 我猜这是你写的queues come and go.