Skip to content
Advertisement

How to inspect and cancel Celery tasks by task name

I’m using Celery (3.0.15) with Redis as a broker.

Is there a straightforward way to query the number of tasks with a given name that exist in a Celery queue?

And, as a followup, is there a way to cancel all tasks with a given name that exist in a Celery queue?

I’ve been through the Monitoring and Management Guide and don’t see a solution there.

Advertisement

Answer

# Retrieve tasks
# Reference: http://docs.celeryproject.org/en/latest/reference/celery.events.state.html
query = celery.events.state.tasks_by_type(your_task_name)

# Kill tasks
# Reference: http://docs.celeryproject.org/en/latest/userguide/workers.html#revoking-tasks
for uuid, task in query:
    celery.control.revoke(uuid, terminate=True)
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement