2011-09-26 115 views
5

我正在使用python芹菜+ rabbitmq。我找不到在某个队列中获取任务数量的方法。 有些事情是这样的:芹菜得到任务计数

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

它是更多钞票来获得任务从certaint队列计数?

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

"rabbitmqctl list_queues -p my_vhost" 

和分析的结果,是它这样做的好方法?

+0

请看[这里](http://celery.readthedocs.org/en/latest/userguide/monitoring.html#inspecting-queues)。 – hymloth

+0

我更新了问题。 – Evg

+0

当然可以运行外部命令来获取其他地方的某些信息...... – hymloth

回答

5

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

通过与鼠对象打我找到工作的解决方案:

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 

我没有找到关于检查AMQP队列鼠兔的文档,所以我不知道解决方案的正确性。