我正在使用芹菜2.4.1与
python 2.6,rabbitmq后端和django.如果工人关闭,我希望我的任务能够正确清理.据我所知,你无法提供任务析构函数,所以我试着勾住
worker_shutdown信号.
注意:AbortableTask仅适用于数据库后端,所以我无法使用它.
from celery.signals import worker_shutdown @task def mytask(*args) obj = DoStuff() def shutdown_hook(*args): print "Worker shutting down" # cleanup nicely obj.stop() worker_shutdown.connect(shutdown_hook) # blocking call that monitors a network connection obj.stuff()
但是,永远不会调用shutdown hook. Ctrl-C’ing工作人员不会杀死任务,我必须从shell手动杀死它.
解决方法
worker_shutdown仅由MainProcess发送,而不是子池worker.
除worker_process_init之外的所有worker_ *信号都引用MainProcess.
除worker_process_init之外的所有worker_ *信号都引用MainProcess.
However,the shutdown hook never gets called. Ctrl-C’ing the worker
doesn’t kill the task and I have to manually kill it from the shell.
工作人员永远不会在正常(暖)关闭下终止任务.
即使任务需要数天才能完成,工作人员也无法完成关闭
直到它完成.您可以将–soft-time-limit或–time-limit设置为
告诉实例什么时候可以终止任务.
因此,首先需要添加任何类型的流程清理过程
确保任务可以实际完成.因为清理不会
在那之前被召唤.
要向池工作进程添加清理步骤,您可以使用
就像是:
from celery import platforms from celery.signals import worker_process_init def cleanup_after_tasks(signum,frame): # reentrant code here (see http://docs.python.org/library/signal.html) def install_pool_process_sighandlers(**kwargs): platforms.signals["TERM"] = cleanup_after_tasks platforms.signals["INT"] = cleanup_after_tasks worker_process_init.connect(install_pool_process_sighandlers)