Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Celery get task count

Tags:

I am currently using django with celery and everything works fine.

However I want to be able to give the users an opportunity to cancel a task if the server is overloaded by checking how many tasks are currently scheduled.

How can I achieve this ?

I am using redis as broker.

I just found this : Retrieve list of tasks in a queue in Celery

It is somehow relate to my issue but I don't need to list the tasks , just count them :)

like image 682
maazza Avatar asked Sep 05 '13 08:09

maazza


People also ask

What is celery beat in Django?

Using django-celery-beatThis extension enables the user to store periodic tasks in a Django database and manage the tasks using the Django Admin interface. Use the following steps to install django-celery-beat in the simpletask project.


1 Answers

Here is how you can get the number of messages in a queue using celery that is broker-agnostic.

By using connection_or_acquire, you can minimize the number of open connections to your broker by utilizing celery's internal connection pooling.

celery = Celery(app)  with celery.connection_or_acquire() as conn:     conn.default_channel.queue_declare(         queue='my-queue', passive=True).message_count 

You can also extend Celery to provide this functionality:

from celery import Celery as _Celery   class Celery(_Celery)      def get_message_count(self, queue):         '''         Raises: amqp.exceptions.NotFound: if queue does not exist         '''         with self.connection_or_acquire() as conn:             return conn.default_channel.queue_declare(                 queue=queue, passive=True).message_count   celery = Celery(app) num_messages = celery.get_message_count('my-queue') 
like image 98
Stephen Fuhry Avatar answered Sep 28 '22 06:09

Stephen Fuhry