这是以编程方式终止(取消)芹菜任务的最佳方法 [英] Which is the best way to programatically terminate (cancel) a celery task

查看:97
本文介绍了这是以编程方式终止(取消)芹菜任务的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据Celery的文档,我们不应在 revoke()函数中使用 terminate 选项来取消正在执行的任务:

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?

推荐答案

如果您确实需要终止正在运行的任务,请在 revoke()方法中使用 terminate = True 是正确的方法,也是您的最佳选择.问题在于这样做并不能保证效果,或者可以说会带来负面影响.要了解原因,有必要知道撤销的工作原理和 terminate = True 的作用.当您调用 revoke()时,您正在通过代理将广播消息发送给您的工作人员.这意味着,在您意识到自己想要/需要杀死任务,发送撤销消息并接收到工作人员的控制命令之前,工作人员可能已经完成了该任务,从队列中抓取了另一个任务并开始进行工作.它.当它最终收到命令时,它只是杀死了工作进程,而该工作进程到那时可能已经在执行其他任务.

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.

这篇关于这是以编程方式终止(取消)芹菜任务的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆