简体   繁体   English

如何在python中使用celery清除特定队列的所有任务?

[英]How to purge all tasks of a specific queue with celery in python?

How to purge all scheduled and running tasks of a specific que with celery in python? 如何在python中使用celery清除特定que的所有计划和运行任务? The questions seems pretty straigtforward, but to add I am not looking for the command line code 问题看起来很简单,但补充一点,我不是在寻找命令行代码

I have the following line, which defines the que and would like to purge that que to manage tasks: 我有以下行,它定义了que并希望清除该que来管理任务:

CELERY_ROUTES = {"socialreport.tasks.twitter_save": {"queue": "twitter_save"}}

At 1 point in time I wanna purge all tasks in the que twitter_save with python code, maybe with a broadcast function? 在1个时间点我想使用python代码清除que twitter_save中的所有任务,也许是使用广播功能? I couldn't find the documentation about this. 我找不到关于此的文档。 Is this possible? 这可能吗?

just to update @Sam Stoelinga answer for celery 3.1, now it can be done like this on a terminal: 只是为了更新@Sam Stoelinga对芹菜3.1的回答,现在可以在终端上这样做:

celery amqp queue.purge <QUEUE_NAME>

For Django be sure to start it from the manage.py file: 对于Django,请务必从manage.py文件启动它:

./manage.py celery amqp queue.purge <QUEUE_NAME> 

If not, be sure celery is able to point correctly to the broker by setting the --broker= flag. 如果没有,请确保芹菜能够通过设置--broker=标志正确指向代理。

The original answer does not work for Celery 3.1. 原始答案不适用于Celery 3.1。 Hassek's update is the correct command if you want to do it from the command line. 如果您想从命令行执行此操作,Hassek的更新是正确的命令。 But if you want to do it programmatically , do this: 但是,如果您想以编程方式执行此操作,请执行以下操作:

Assuming you ran your Celery app as: 假设您将Celery应用程序运行为:

celery_app = Celery(...)

Then: 然后:

import celery.bin.amqp
amqp = celery.bin.amqp.amqp(app = celery_app)
amqp.run('queue.purge', 'name_of_your_queue')

This is handy for cases where you've enqueued a bunch of tasks, and one task encounters a fatal condition that you know will prevent the rest of the tasks from executing. 这对于您排队了大量任务的情况非常方便,而且一个任务遇到致命条件,您知道这会阻止其他任务执行。

Eg you enqueued a bunch of web crawler tasks, and in the middle of your tasks your server's IP address gets blocked. 例如,您排队了一堆网络爬虫任务,并且在您的任务中间,您的服务器的IP地址被阻止。 There's no point in executing the rest of the tasks. 执行其余任务没有意义。 So in that case, your task it self can purge its own queue. 所以在这种情况下,你自己的任务可以清除自己的队列。

Lol it's quite easy, hope somebody can help me still though. 大声笑很容易,希望有人可以帮助我。

from celery.bin.camqadm import camqadm
camqadm('queue.purge', queue_name_as_string)

The only problem with this I still need to stop the celeryd before purging the que, after purging I need to run the celeryd again to handle tasks for the queue. 唯一的问题是我仍然需要在清除que之前停止celeryd,在清除之后我需要再次运行celeryd来处理队列的任务。 Will update this question if i succeed. 如果我成功,将更新此问题。

I succeeded, but please correct me if this is not a good method to stop the celeryd, purge que and start it again. 我成功了,但请纠正我,如果这不是一个很好的方法来阻止芹菜,清除阙并再次启动它。 I know I am using term, because I actually want it to be terminated the task. 我知道我正在使用术语,因为我实际上希望它终止任务。

kill_command =  "ps auxww | grep 'celeryd -n twitter_save' | awk '{print $2}' | xargs kill -9"
subprocess.call(kill_command, shell=True)

camqadm('queue.purge', 'twitter_save')
rerun_command = "/home/samos/Software/virt_env/twittersyncv1/bin/python %s/manage.py celeryd -n twitter_save -l info -Q twitter_save" % settings.PROJECT_ROOT

os.popen(rerun_command+' &')
send_task("socialreport.tasks.twitter_save")

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

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