Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 list_queues -p my_vhost"

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

like image 582
Evg Avatar asked Sep 26 '11 08:09

Evg


People also ask

How do I check my celery queue?

Just to spell things out, the DATABASE_NUMBER used by default is 0 , and the QUEUE_NAME is celery , so redis-cli -n 0 llen celery will return the number of queued messages.

How does celery execute tasks?

Once you integrate Celery into your app, you can send time-intensive tasks to Celery's task queue. That way, your web app can continue to respond quickly to users while Celery completes expensive operations asynchronously in the background.

Where are celery tasks stored?

In Celery, a result back end is a place where, when you call a Celery task with a return statement, the task results are stored.


1 Answers

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.

like image 78
Aleksey Avatar answered Oct 01 '22 15:10

Aleksey