Django Celery gets a task counter

I am currently using django with celery and everything is working fine.

However, I want to be able to give users the option to cancel the task if the server is overloaded, checking how many tasks are currently scheduled.

How can i achieve this?

I use redis as a broker.

I just found this: Get a list of tasks queued in Celery

This is somehow related to my problem, but I do not need to list the tasks, just read them :)

+6
source share
3 answers

If your broker is configured as redis://localhost:6379/1 , and your tasks are sent to the general celery , you can get the length using the following tools:

 import redis queue_name = "celery" client = redis.Redis(host="localhost", port=6379, db=1) length = client.llen(queue_name) 

Or, from a shell script (good for monitors, etc.):

 $ redis-cli -n 1 -h localhost -p 6379 llen celery 
+8
source

Here's how you can get the number of messages in a queue using celery, which is an intermediary agent.

Using connection_or_acquire , you can minimize the number of open connections to your broker by using the union of internal celery connections.

 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 feature:

 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') 
+7
source

If you have already configured redis in your application, you can try the following:

 from celery import Celery QUEUE_NAME = 'celery' celery = Celery(app) client = celery.connection().channel().client length = client.llen(QUEUE_NAME) 
+4
source

Source: https://habr.com/ru/post/953216/


All Articles