Revoke a task from celery

橙三吉。 提交于 2020-07-04 20:56:11

问题


I want to explicitly revoke a task from celery. This is how I'm currently doing:-

from celery.task.control import revoke

revoke(task_id, terminate=True)

where task_id is string(have also tried converting it into UUID uuid.UUID(task_id).hex).

After the above procedure, when I start celery again celery worker -A proj it still consumes the same message and starts processing it. Why?

When viewed via flower, the message is still there in the broker section. how do I delete the message so that it cant be consumed again?


回答1:


How does revoke works?

When calling the revoke method the task doesn't get deleted from the queue immediately, all it does is tell celery(not your broker!) to save the task_id in a in-memory set(look here if you like reading source code like me).

When the task gets to the top of the queue, Celery will check if is it in the revoked set, if it does, it won't execute it.

It works this way to prevent O(n) search for each revoke call, where checking if the task_id is in the in-memory set is just O(1)

Why after restarting celery, your revoked tasks executed?

Understanding how things works, you now know that the set is just a normal python set, that being saved in-memory - that means when you restart, you lose this set, but the task is(of course) persistence and when the tasks turn comes, it will be executed as normal.

What can you do?

You will need to have a persistence set, this is done by initial your worker like this:

celery worker -A proj --statedb=/var/run/celery/worker.state

This will save the set on the filesystem.

References:

  • Celery source code of the in-memory set
  • Revoke doc
  • Persistent revokes docs


来源:https://stackoverflow.com/questions/39191238/revoke-a-task-from-celery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!