celery 获取任务数

发布于 2024-12-06 11:49:40 字数 280 浏览 4 评论 0原文

我正在使用 python celery+rabbitmq。我找不到一种方法来获取某个队列中的任务计数。 像这样的事情:

celery.queue('myqueue').count()

是否可以从某个队列中获取任务计数?

一种解决方案是从我的 python scrpit: 运行外部命令

"rabbitmqctl list_queues -p my_vhost"

并解析结果,这是一个好方法吗?

I am using python celery+rabbitmq. I can't find a way to get task count in some queue.
Some thing like this:

celery.queue('myqueue').count()

Is it posible to get tasks count from certaint queue?

One solution is to run external command from my python scrpit:

"rabbitmqctl list_queues -p my_vhost"

and parse results, is it good way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤单情人 2024-12-13 11:49:41

我认为使用rabbitmqctl命令不是一个好的解决方案,特别是在我的ubuntu服务器上,rabbitmqctl只能以root权限执行。

通过使用 pika 对象,我找到了可行的解决方案:

import pika
from django.conf import settings 

def tasks_count(queue_name):
    ''' Connects to message queue using django settings and returns count of messages in queue with name queue_name. '''
    credentials = pika.PlainCredentials(settings.BROKER_USER, settings.BROKER_PASSWORD)
    parameters = pika.ConnectionParameters( credentials=credentials,
                                           host=settings.BROKER_HOST,
                                           port=settings.BROKER_PORT,
                                           virtual_host=settings.BROKER_VHOST)
    connection = pika.BlockingConnection(parameters=parameters)
    channel = connection.channel()
    queue = channel.queue_declare(queue=queue_name, durable=True)
    message_count = queue.method.message_count
    return message_count

我没有找到有关使用 pika 检查 AMQP 队列的文档,因此我不知道解决方案的正确性。

I suppose that using rabbitmqctl command is not good solution, especially on my ubuntu server, where rabbitmqctl can be executed only with root privileges.

By playing with pika objects I found working solution:

import pika
from django.conf import settings 

def tasks_count(queue_name):
    ''' Connects to message queue using django settings and returns count of messages in queue with name queue_name. '''
    credentials = pika.PlainCredentials(settings.BROKER_USER, settings.BROKER_PASSWORD)
    parameters = pika.ConnectionParameters( credentials=credentials,
                                           host=settings.BROKER_HOST,
                                           port=settings.BROKER_PORT,
                                           virtual_host=settings.BROKER_VHOST)
    connection = pika.BlockingConnection(parameters=parameters)
    channel = connection.channel()
    queue = channel.queue_declare(queue=queue_name, durable=True)
    message_count = queue.method.message_count
    return message_count

I did not find documentation about inspecting the AMQP queue with pika, so I do not know about solution's correctness.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文