How can I retrieve a list of tasks in a queue that are yet to be processed?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
I think the only way to get the tasks that are waiting is to keep a list of tasks you started and let the task remove itself from the list when it's started.
With rabbitmqctl and list_queues you can get an overview of how many tasks are waiting, but not the tasks itself: http://www.rabbitmq.com/man/rabbitmqctl.1.man.html
If what you want includes the task being processed, but are not finished yet, you can keep a list of you tasks and check their states:
Or you let Celery store the results with CELERY_RESULT_BACKEND and check which of your tasks are not in there.
If you control the code of the tasks then you can work around the problem by letting a task trigger a trivial retry the first time it executes, then checking
inspect().reserved()
. The retry registers the task with the result backend, and celery can see that. The task must acceptself
orcontext
as first parameter so we can access the retry count.This solution is broker agnostic, ie. you don't have to worry about whether you are using RabbitMQ or Redis to store the tasks.
EDIT: after testing I've found this to be only a partial solution. The size of reserved is limited to the prefetch setting for the worker.
EDIT: See other answers for getting a list of tasks in the queue.
You should look here: Celery Guide - Inspecting Workers
Basically this:
Depending on what you want
if you are using rabbitMQ, use this in terminal:
it will print list of queues with number of pending tasks. for example:
the number in right column is number of tasks in the queue. in above, celery queue has 166 pending task.
To retrieve tasks from backend, use this
If you don't use prioritized tasks, this is actually pretty simple if you're using Redis. To get the task counts:
But, prioritized tasks use a different key in redis, so the full picture is slightly more complicated. The full picture is that you need to query redis for every priority of task. In python (and from the Flower project), this looks like:
If you want to get an actual task, you can use something like:
From there you'll have to deserialize the returned list. In my case I was able to accomplish this with something like:
Just be warned that deserialization can take a moment, and you'll need to adjust the commands above to work with various priorities.