celery get tasks count
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 lis开发者_如何学运维t_queues -p my_vhost"
and parse results, is it good way to do this?
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.
 
         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论