Python 如何按任务名称检查和取消 Celery 任务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15575826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 20:27:02  来源:igfitidea点击:

How to inspect and cancel Celery tasks by task name

pythonrediscelery

提问by Mzzzzzz

I'm using Celery (3.0.15) with Redis as a broker.

我使用 Celery (3.0.15) 和 Redis 作为代理。

Is there a straightforward way to query the number of tasks with a given name that exist in a Celery queue?

有没有一种直接的方法来查询 Celery 队列中存在的给定名称的任务数量?

And, as a followup, is there a way to cancel all tasks with a given name that exist in a Celery queue?

而且,作为后续,有没有办法取消 Celery 队列中存在的具有给定名称的所有任务?

I've been through the Monitoring and Management Guideand don't see a solution there.

我已经阅读了《监控和管理指南》,但在那里没有看到解决方案。

采纳答案by gioi

# Retrieve tasks
# Reference: http://docs.celeryproject.org/en/latest/reference/celery.events.state.html
query = celery.events.state.tasks_by_type(your_task_name)

# Kill tasks
# Reference: http://docs.celeryproject.org/en/latest/userguide/workers.html#revoking-tasks
for uuid, task in query:
    celery.control.revoke(uuid, terminate=True)

回答by dm03514

It looks like flowerprovides monitoring:

它看起来像flower提供监控:

https://github.com/mher/flower

https://github.com/mher/flower

Real-time monitoring using Celery Events

Task progress and history Ability to show task details (arguments, start time, runtime, and more) Graphs and statistics Remote Control

View worker status and statistics Shutdown and restart worker instances Control worker pool size and autoscale settings View and modify the queues a worker instance consumes from View currently running tasks View scheduled tasks (ETA/countdown) View reserved and revoked tasks Apply time and rate limits Configuration viewer Revoke or terminate tasks HTTP API

OpenID authentication

使用 Celery Events 进行实时监控

任务进度和历史 能够显示任务详细信息(参数、开始时间、运行时间等) 图表和统计数据 远程控制

查看工作状态和统计信息 关闭和重启工作实例 控制工作池大小和自动缩放设置 查看和修改工作实例使用的队列 查看当前正在运行的任务 查看计划任务(ETA/倒计时) 查看保留和撤销的任务 应用时间和速率限制 配置查看器撤销或终止任务 HTTP API

OpenID 认证

回答by Danielle Madeley

You can do this in one request:

您可以在一个请求中执行此操作:

app.control.revoke([
    uuid
    for uuid, _ in
    celery.events.state.State().tasks_by_type(task_name)
])

回答by Louis

There is one issue that earlier answers have not addressed and may throw off people if they are not aware of it.

有一个问题是之前的答案没有解决的,如果人们不知道,可能会让他们望而却步。

Among those solutions already posted, I'd use Danielle'swith one minor modification: I'd import the task into my file and use its .nameattribute to get the task name to pass to .tasks_by_type().

在已经发布的这些解决方案中,我将使用Danielle 的一个小修改:我将任务导入到我的文件中,并使用它的.name属性将任务名称传递给.tasks_by_type().

app.control.revoke(
    [uuid for uuid, _ in
     celery.events.state.State().tasks_by_type(task.name)])

However, this solution will ignore those tasks that have been scheduled for future execution.Like some people who commented on other answers, when I checked what .tasks_by_type()return I had an empty list. And indeed my queues were empty. But I knew that there were tasks scheduled to be executed in the future and thesewere my primary target. I could see them by executing celery -A [app] inspect scheduledbut they were unaffected by the code above.

但是,此解决方案将忽略那些已安排在将来执行的任务。就像一些评论其他答案的人一样,当我检查什么.tasks_by_type()返回时,我有一个空列表。确实我的队列是空的。但我知道有计划在未来执行的任务,这些是我的主要目标。我可以通过执行看到它们,celery -A [app] inspect scheduled但它们不受上面代码的影响。

I managed to revoke the scheduled tasks by doing this:

我设法通过这样做撤销了计划任务:

app.control.revoke(
    [scheduled["request"]["id"] for scheduled in
     chain.from_iterable(app.control.inspect().scheduled()
                         .itervalues())])

app.control.inspect().scheduled()returns a dictionary whose keys are worker names and values are listsof scheduling information (hence, the need for chain.from_iterablewhich is imported from itertools). The task information is in the "request"field of the scheduling information and "id"contains the task id. Note that even after revocation, the scheduled task will still show among the scheduled tasks. Scheduled tasks that are revoked won't get removed from the list of scheduled tasks until their timers expire or until Celery performs some cleanup operation. (Restarting workers triggers such cleanup.)

app.control.inspect().scheduled()返回一个字典,其键是工作人员名称,值是调度信息列表(因此,需要chain.from_iterable从 导入itertools)。任务信息在"request"调度信息字段中,"id"包含任务id。请注意,即使在撤销后,计划任务仍会显示在计划任务中。被撤销的计划任务不会从计划任务列表中删除,直到它们的计时器到期或直到 Celery 执行一些清理操作。(重新启动工作人员会触发此类清理。)