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 :)
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.
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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With