简体   繁体   English

从芹菜中撤销任务

[英]Revoke a task from celery

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)

where task_id is string (have also tried converting it into UUID uuid.UUID(task_id).hex) . 其中task_id是string (还尝试将其转换为UUID uuid.UUID(task_id).hex)

After the above procedure, when I start celery again celery worker -A proj it still consumes the same message and starts processing it. 完成上述步骤后,当我再次启动celery时, celery worker -A proj仍会消耗相同的消息并开始处理它。 Why? 为什么?

When viewed via flower , the message is still there in the broker section. 当通过flower查看时,消息仍在代理部分中。 how do I delete the message so that it cant be consumed again? 如何删除该消息,以使它不再被使用?

How does revoke works? revoke如何运作?

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). 当调用revoke方法时,任务不会立即从队列中删除,它所做的只是告诉celery(不是您的代理!)将task_id保存在内存set (如果您喜欢阅读像我这样的源代码,请查看此处) )。

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. 当任务到达队列的顶部时,Celery将检查该任务是否在已撤消的集合中,如果存在,将不会执行。

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) 它以这种方式工作,以防止O(n)搜索每个revoke调用,其中检查task_id是否在内存集中仅是O(1)

Why after restarting celery, your revoked tasks executed? 为什么重启芹菜后,您撤销的任务执行了?

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. 了解事物的工作原理后,您现在知道该set只是一个普通的python集合,已保存在内存中 -这意味着当您重新启动时,会丢失此集合,但是任务(当然)是持久性的,并且当任务转来,它将照常执行。

What can you do? 你能做什么?

You will need to have a persistence set, this is done by initial your worker like this: 您将需要设置一个持久性集,这是通过最初的工作人员完成的,如下所示:

celery worker -A proj --statedb=/var/run/celery/worker.state

This will save the set on the filesystem. 这会将设置保存在文件系统上。

References: 参考文献:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM