0

I have a Celery task called my_task. I create multiple exeucutions of the task by calling my_task.delay() multiple times. I want to give a unique label/name to each execution, and to be able to get and stop a task execution for a given label/name. How can I do something like this?

1 Answer 1

1

Tasks are assigned an ID when created. To revoke a task, you can just use the revoke method on the task object itself or you can call app.control.revoke with the ID of the task.

task = my_task.delay()
print(task.id)  # a string identifier like abc1234-5678-4bec-b71a-665ba5d23004
# cancel _this_ task
task.revoke()
# or
app.control.revoke(task.id)

If you really want to refer to these by a plain name, you can store a mapping of names to IDs...

tasks = {} # a mapping to store names to tasks (or task IDs)
task = my_task.delay()
name = 'foo'
tasks[name] = task.id
# then later, decide you want to cancel task 'foo'
...
foo_task_id = tasks['foo']
app.control.revoke(foo_task_id)

This assumes you are using a broker that supports revocation of tasks.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.