Get waiting tasks count in Celery

If you’re using Redis as broker, you can simply run the following command to get the number of waiting tasks in a queue:

$ redis-cli -h HOST -p PORT -n DATABASE_NUMBER llen QUEUE_NAME

The default queue is named celery. It’s a list so you can get its length with the llen command.

Celery provides the inspect reserved command, and what you’ll get is a list of tasks that have been prefetched by the worker. By default, the prefetch count is worker_prefetch_multiplier (default is 4) multiplied by the number of concurrent worker processes.

$ celery -A proj -b redis:// inspect reserved
->  celery@worker1: OK
    * {'id': 'e7a31ff3-375c-4afe-a93c-7a151b8e898b', 'name': ...}
    * {'id': '87d63fe1-8133-4b50-86fc-d7a6a1dcff75', 'name': ...}
    * {'id': 'dbb4141b-0f89-48fa-87fc-e8bcfae432b0', 'name': ...}
    * {'id': '0299a9c5-9ff7-4bec-8bb7-fa88f7067a9b', 'name': ...}

So which queue are these tasks stored in?

Let’s run redis-cli in interactive mode, then select the database used by broker and list all keys:> keys *
1) "celery"
2) "_kombu.binding.celeryev"
3) "unacked"
4) "unacked_index"
5) "_kombu.binding.celery"
6) "_kombu.binding.celery.pidbox"

You can see a queue named unacked.

unacked is a hash, you can get the length of it by using the hlen command:> type unacked
hash> hlen unacked
(integer) 4

And use the hgetall command to get all the fields and values:> hgetall unacked
1) "8a64f4bb-2fd3-4d37-a273-8fd136190dcf"
2) "[{\"body\": \"\", \"content-encoding\": \"utf-8\", \"content-type\": \"application/json\", \"headers\": {\"lang\": \"py\", \"task\": \"mytask\", \"id\": \"0299a9c5-9ff7-4bec-8bb7-fa88f7067a9b\", ...]"

Note that the id shown in the value is the same as the one you got with the inspect reserved command. Reserved tasks are stored in the unacked queue.

By the way, unacked_index is a zset (sorted set) with timestamps as scores.> type unacked_index
zset> zrange unacked_index 0 -1 withscores
1) "13ad4b23-a283-48ee-a177-06f65b519af7"
2) "1661418249.2947526"
3) "8a64f4bb-2fd3-4d37-a273-8fd136190dcf"
4) "1661418249.2968478"