0%

Get Celery Queue Length and Purge a Queue

I recently wanted a Django-based system to display how many Celery tasks were currently backed up, and also provide a way to clear a queue. Since we were using RabbitMQ as the Celery broker, one option was to build everything directly with Kombu, but there is a simpler approach that avoids writing too much broker-specific code.

To get the number of pending messages in a queue, use code like this. Replace your_app_name and your_queue_name with the real values from your environment:

1
2
3
4
5
6
from celery import Celery

app = Celery('your_app_name')
queue_info = app.connection().channel().queue_declare(your_queue_name, passive=True)

print('message count:', queue_info.message_count)

One thing to note is that this will raise an error if the queue does not exist.

If you want to purge the queue, change queue_declare to queue_purge:

1
app.connection().channel().queue_purge(your_queue_name)
如果我的文字帮到了您,那么可不可以请我喝罐可乐?