gpt4 book ai didi

python - 这是以编程方式终止(取消) celery 任务的最佳方式

转载 作者:太空宇宙 更新时间:2023-11-04 00:10:17 38 4
gpt4 key购买 nike

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

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

所以,我的问题是,如果我需要以编程方式取消一些正在运行的任务,我应该如何正确操作?

最佳答案

如果您确实需要终止正在运行的任务,在revoke() 方法中使用terminate=True 是正确的方法,也是您的最佳选择。问题是这样做并不能保证效果,或者更好地说,可能会产生负面影响。要了解原因,有必要了解撤销的工作原理以及 terminate=True 的作用。当您调用 revoke() 时,您正在通过代理向您的工作人员发送广播消息。这意味着在你意识到你想要/需要终止任务、发送撤销消息并接收工作人员的控制命令之前,工作人员可能已经完成了任务,从队列中抓取另一个任务并开始工作它。当它最终收到命令时,它只是终止工作进程,到那时,该进程可能已经在处理不同的任务。

如果你事先知道你可能需要中止你的任务并且可以调整任务代码,你可以看看abortable tasks .

关于python - 这是以编程方式终止(取消) celery 任务的最佳方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52724789/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com