python – 通知工作者关闭的芹菜任务

前端之家收集整理的这篇文章主要介绍了python – 通知工作者关闭的芹菜任务前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用芹菜2.4.1与 python 2.6,rabbitmq后端和django.如果工人关闭,我希望我的任务能够正确清理.据我所知,你无法提供任务析构函数,所以我试着勾住 worker_shutdown信号.

注意:AbortableTask仅适用于数据库后端,所以我无法使用它.

  1. from celery.signals import worker_shutdown
  2.  
  3. @task
  4. def mytask(*args)
  5.  
  6. obj = DoStuff()
  7.  
  8. def shutdown_hook(*args):
  9. print "Worker shutting down"
  10. # cleanup nicely
  11. obj.stop()
  12.  
  13. worker_shutdown.connect(shutdown_hook)
  14.  
  15. # blocking call that monitors a network connection
  16. obj.stuff()

但是,永远不会调用shutdown hook. Ctrl-C’ing工作人员不会杀死任务,我必须从shell手动杀死它.

因此,如果这不是正确的方法,我如何允许任务正常关闭

解决方法

worker_shutdown仅由MainProcess发送,而不是子池worker.
除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设置为
告诉实例什么时候可以终止任务.

因此,首先需要添加任何类型的流程清理过程
确保任务可以实际完成.因为清理不会
在那之前被召唤.

要向池工作进程添加清理步骤,您可以使用
就像是:

  1. from celery import platforms
  2. from celery.signals import worker_process_init
  3.  
  4. def cleanup_after_tasks(signum,frame):
  5. # reentrant code here (see http://docs.python.org/library/signal.html)
  6.  
  7. def install_pool_process_sighandlers(**kwargs):
  8. platforms.signals["TERM"] = cleanup_after_tasks
  9. platforms.signals["INT"] = cleanup_after_tasks
  10.  
  11. worker_process_init.connect(install_pool_process_sighandlers)

猜你在找的Python相关文章