如何使用 python 列出或发现 RabbitMQ 交换上的队列?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4287941/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 15:04:55  来源:igfitidea点击:

How can I list or discover queues on a RabbitMQ exchange using python?

pythonrabbitmqamqp

提问by rschramm

I need to have a python client that can discover queues on a restarted RabbitMQ server exchange, and then start up a clients to resume consuming messages from each queue. How can I discover queues from some RabbitMQ compatible python api/library?

我需要一个 python 客户端,它可以在重新启动的 RabbitMQ 服务器交换上发现队列,然后启动一个客户端以恢复使用每个队列中的消息。如何从一些 RabbitMQ 兼容的 python api/library 中发现队列?

采纳答案by Daniel Roseman

As far as I know, there isn't any way of doing this. That's nothing to do with Python, but because AMQP doesn't define any method of queue discovery.

据我所知,没有任何方法可以做到这一点。这与 Python 无关,而是因为 AMQP 没有定义任何队列发现方法。

In any case, in AMQP it's clients (consumers) that declare queues: publishers publish messages to an exchange with a routing key, and consumers determine which queues those routing keys go to. So it does not make sense to talk about queues in the absence of consumers.

无论如何,在 AMQP 中,声明队列的是客户端(消费者):发布者将消息发布到带有路由键的交换,消费者确定这些路由键去哪个队列。所以在没有消费者的情况下谈论队列是没有意义的。

回答by mariana soffer

Management features are due in a future version of AMQP. So for now you will have to wait till for a new version that will come with that functionality.

管理功能将在 AMQP 的未来版本中提供。因此,现在您将不得不等待具有该功能的新版本。

回答by olleolleolle

Since I am a RabbitMQ beginner, take this with a grain of salt, but there's an interesting Management Plugin, which exposes an HTTP interface to "From here you can manage exchanges, queues, bindings, virtual hosts, users and permissions. Hopefully the UI is fairly self-explanatory."

由于我是 RabbitMQ 初学者,因此请稍加保留,但是有一个有趣的Management Plugin,它公开了一个 HTTP 接口“从这里你可以管理交换、队列、绑定、虚拟主机、用户和权限。希望 UI是不言自明的。”

http://www.rabbitmq.com/blog/2010/09/07/management-plugin-preview-release/

http://www.rabbitmq.com/blog/2010/09/07/management-plugin-preview-release/

回答by IvanD

There does not seem to be a direct AMQP-way to manage the server but there is a way you can do it from Python. I would recommend using a subprocessmodule combined with the rabbitmqctlcommand to check the status of the queues.

似乎没有直接的 AMQP 方式来管理服务器,但有一种方法可以从 Python 中完成。我建议将子进程模块与rabbitmqctl命令结合使用来检查队列的状态。

I am assuming that you are running this on Linux. From a command line, running:

我假设您在 Linux 上运行它。从命令行,运行:

rabbitmqctl list_queues

will result in:

将导致:

Listing queues ...
pings   0
receptions      0
shoveled        0
test1   55199
...done.

(well, it did in my case due to my specific queues)

(好吧,由于我的特定队列,在我的情况下确实如此)

In your code, use this code to get output of rabbitmqctl:

在您的代码中,使用此代码获取以下输出rabbitmqctl

import subprocess

proc = subprocess.Popen("/usr/sbin/rabbitmqctl list_queues", shell=True, stdout=subprocess.PIPE)
stdout_value = proc.communicate()[0]
print stdout_value

Then, just come up with your own code to parse stdout_valuefor your own use.

然后,只需拿出您自己的代码进行解析stdout_value以供您自己使用。

回答by David Lemphers

I use https://github.com/bkjones/pyrabbit. It's talks directly to RabbitMQ's mgmt plugin's API interface, and is very handy for interrogating RabbitMQ.

我使用https://github.com/bkjones/pyrabbit。它直接与 RabbitMQ 的 mgmt 插件的 API 接口对话,对于查询 RabbitMQ 非常方便。

回答by diogobaeder

pyrabbit didn't work so well for me; However, the Management Plugin itself has its own command line script that you can download from your own admin GUI and use later on (for example, I downloaded mine from

pyrabbit 对我来说效果不佳;但是,管理插件本身有自己的命令行脚本,您可以从自己的管理 GUI 下载该脚本并稍后使用(例如,我从

http://localhost:15672/cli/

for local use)

供本地使用)

回答by Sergey Telminov

You can add plugin rabbitmq_management

你可以添加插件rabbitmq_management

sudo /usr/lib/rabbitmq/bin/rabbitmq-plugins enable rabbitmq_management
sudo service rabbitmq-server restart

Then use rest-api

然后使用rest-api

import requests

def rest_queue_list(user='guest', password='guest', host='localhost', port=15672, virtual_host=None):
    url = 'http://%s:%s/api/queues/%s' % (host, port, virtual_host or '')
    response = requests.get(url, auth=(user, password))
    queues = [q['name'] for q in response.json()]
    return queues

I'm using requestslibrary in this example, but it is not significantly.

我在这个例子中使用了requests库,但它并不重要。

Also I found library that do it for us - pyrabbit

我还找到了为我们做这件事的图书馆 - pyrabbit

from pyrabbit.api import Client
cl = Client('localhost:15672', 'guest', 'guest')
queues = [q['name'] for q in cl.get_queues()]