从芹菜中撤消任务 [英] Revoke a task from celery

查看:65
本文介绍了从芹菜中撤消任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想明确撤消芹菜的一项任务.这是我目前的工作方式:-

I want to explicitly revoke a task from celery. This is how I'm currently doing:-

from celery.task.control import revoke

revoke(task_id, terminate=True)

其中task_id是 string (还尝试将其转换为UUID uuid.UUID(task_id).hex).

where task_id is string(have also tried converting it into UUID uuid.UUID(task_id).hex).

完成上述步骤后,当我再次启动celery时[celery worker -A proj ]仍然会消耗相同的消息并开始对其进行处理.为什么?

After the above procedure, when I start celery again celery worker -A proj it still consumes the same message and starts processing it. Why?

通过 flower 查看时,消息仍在中间人部分中.如何删除该消息,以使它不再被使用?

When viewed via flower, the message is still there in the broker section. how do I delete the message so that it cant be consumed again?

推荐答案

撤销如何工作?

调用 revoke 方法时,任务不会立即从队列中删除,它所做的只是告诉celery(不是您的经纪人!)保存 task_id 在内存中的 set 中(查看

How does revoke works?

When calling the revoke method the task doesn't get deleted from the queue immediately, all it does is tell celery(not your broker!) to save the task_id in a in-memory set(look here if you like reading source code like me).

当任务到达队列的顶部时,Celery将检查该任务是否在已撤消的集合中,如果存在,它将不会执行.

When the task gets to the top of the queue, Celery will check if is it in the revoked set, if it does, it won't execute it.

通过这种方式可以防止O(n)搜索每个 revoke 调用,其中检查task_id是否在内存集中只是O(1)

It works this way to prevent O(n) search for each revoke call, where checking if the task_id is in the in-memory set is just O(1)

了解事物的工作原理,您现在知道 set 只是一个普通的python集,被保存在内存中-这意味着当您重新启动时,您会迷失方向这个集合,但是任务(当然)是持久性的,当任务轮到时,它将照常执行.

Understanding how things works, you now know that the set is just a normal python set, that being saved in-memory - that means when you restart, you lose this set, but the task is(of course) persistence and when the tasks turn comes, it will be executed as normal.

您将需要一个持久性设置,这是通过像这样的初始工作人员来完成的:

You will need to have a persistence set, this is done by initial your worker like this:

芹菜工人-项目--statedb =/var/run/celery/worker.state

这会将设置保存在文件系统上.

This will save the set on the filesystem.

参考文献:

这篇关于从芹菜中撤消任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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