Which is the best way to programatically terminate (cancel) a celery task

两盒软妹~` 提交于 2021-02-07 09:36:27

问题


According to Celery's documentation, we should not use the terminate option in revoke() function to cancel an executing task:

The terminate option is a last resort for administrators when a task is stuck. It’s not for terminating the task, it’s for terminating the process that’s executing the task, and that process may have already started processing another task at the point when the signal is sent, so for this reason you must never call this programmatically.

http://docs.celeryproject.org/en/latest/userguide/workers.html#revoke-revoking-tasks

So, my question is, how should I properly do if I need to cancel some running tasks programmatically?


回答1:


If you really need to kill the running task, using terminate=True in revoke() method is the proper way and your best option. The problem is that doing this has no guaranteed effect or, better said, can have negative side effects. To understand why, it's necessary to know how revoking works and what terminate=True does. When you call revoke(), you are sending broadcast message to your workers via the broker. That means that before the time you realize you want/need to to kill the task, sending the revoke message and receiving the control command by the worker, the worker might have already finished the task, grabbed another task from the queue and started working on it. When it finally receives the command, it just kills the worker process, which, by that time, might already be working on different task.

If you know beforehand you might need to abort your tasks and can adjust the tasks code, you can take a look at abortable tasks.



来源:https://stackoverflow.com/questions/52724789/which-is-the-best-way-to-programatically-terminate-cancel-a-celery-task

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