本文介绍了用Celery(RabbitMQ,Django)检索队列长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个django项目中使用Celery,我的代理是RabbitMQ,我想检索队列的长度。我经历了芹菜的代码,但没有找到这样的工具。我在stackoverflow上发现了这个问题(),但是我没有发现它令人满意。



一切都在芹菜中设置,所以应该有一些魔术方法来检索我想要的,而不需要指定一个频道/连接



有没有人对此问题有任何想法?



谢谢!

解决方案

下面是一个关于如何在给定队列中读取rabbitMQ队列长度的示例:

  def get_rabbitmq_queue_length(q):
from pyrabbit.api import Client
from pyrabbit.http import HTTPError

count = 0

try:
cl = Client('localhost:15672','guest','guest')
如果cl.is_alive():
count = cl.get_queue_depth(' /',q)
除了HTTPError作为e:
打印异常:无法建立到rabbitmq http api:+ str(e)+检查端口,代理,用户名/通配置错误
raise

返回计数

这是使用,如以前由所建议的那样


I'm using Celery in a django project, my broker is RabbitMQ, and I want to retrieve the length of the queues. I went through the code of Celery but did not find the tool to do that. I found this issue on stackoverflow (Check RabbitMQ queue size from client), but I don't find it satisfying.

Everything is setup in celery, so there should be some kind of magic method to retrieve what I want, without specifying a channel / connection.

Does anyone have any idea about this question ?

Thanks !

解决方案

Here is an example on how to read the queue length in rabbitMQ for a given queue:

def get_rabbitmq_queue_length(q):
    from pyrabbit.api import Client
    from pyrabbit.http import HTTPError

    count = 0

    try:
        cl = Client('localhost:15672', 'guest', 'guest')
        if cl.is_alive():
            count = cl.get_queue_depth('/', q)
    except HTTPError as e:
        print "Exception: Could not establish to rabbitmq http api: " + str(e) + " Check for port, proxy, username/pass configuration errors"
        raise

    return count

This is using pyrabbit as previously suggested by Philip

这篇关于用Celery(RabbitMQ,Django)检索队列长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 01:04