用 Celery 取消一个已经在执行的任务? [英] Cancel an already executing task with Celery?
问题描述
我一直在阅读文档并进行搜索,但似乎无法找到直接的答案:
你能取消一个已经在执行的任务吗?(因为在任务已经开始,需要一段时间,中途需要取消)
我从 芹菜常见问题
<预><代码>>>>结果 = add.apply_async(args=[2, 2], 倒计时=120)>>>结果.revoke()但我不清楚这是否会取消排队的任务,或者是否会杀死一个正在运行的进程.感谢您提供的任何光线!
revoke 取消任务执行.如果任务被撤销,工作人员会忽略该任务并且不执行它.如果你不使用persistent revokes,你的任务可以在worker重启后执行.
http://docs.celeryproject.org/en/latest/userguide/workers.html#worker-persistent-revokes
revoke 有一个终止选项,默认为 False.如果您需要终止正在执行的任务,您需要将 terminate 设置为 True.
<预><代码>>>>从 celery.task.control 导入撤销>>>撤销(task_id,终止=真)http://docs.celeryproject.org/en/latest/userguide/workers.html#revoke-revoking-tasks
I have been reading the doc and searching but cannot seem to find a straight answer:
Can you cancel an already executing task? (as in the task has started, takes a while, and half way through it needs to be cancelled)
I found this from the doc at Celery FAQ
>>> result = add.apply_async(args=[2, 2], countdown=120)
>>> result.revoke()
But I am unclear if this will cancel queued tasks or if it will kill a running process on a worker. Thanks for any light you can shed!
revoke cancels the task execution. If a task is revoked, the workers ignore the task and do not execute it. If you don't use persistent revokes your task can be executed after worker's restart.
http://docs.celeryproject.org/en/latest/userguide/workers.html#worker-persistent-revokes
revoke has an terminate option which is False by default. If you need to kill the executing task you need to set terminate to True.
>>> from celery.task.control import revoke
>>> revoke(task_id, terminate=True)
http://docs.celeryproject.org/en/latest/userguide/workers.html#revoke-revoking-tasks
这篇关于用 Celery 取消一个已经在执行的任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!