求教celery如何根据id停止正在执行的任务

当任务运行时间长的时候,我有的时候可能想会停止这个任务进行调整,请问如何根据celery任务的id停止任务呢?
我在文档中找到了增加base=AbortableTask的方式,可以通过AsyncResult发送abort()方法,但是文档中有句话没有看懂,原文是After the result.abort() call, the task execution isn’t aborted immediately. In fact, it’s not guaranteed to abort at all. Keep checking result.state status, or call result.get(timeout=) to have it block until the task is finished.
烦请celery大神帮忙看一下,我找的地方是否正确,这句话又是指什么呢…
另外,文档中提示使用AbortableTask必须使用database backend,那么我使用redis应该是没问题的吧…

阅读 13.4k
2 个回答
from celery.task.control import revoke

revoke(id, terminate=True)

当任务运行时间长的时候,我有的时候可能想会停止这个任务进行调整

如果只是这个目的,加一个time_limit的修饰器不就可以了?

@task(time_limit=20)
def your_task():
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题