通知 celery 任务worker 关闭
我使用 celery 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()
但是,关闭挂钩永远不会被调用。 Ctrl-C'ing 工作线程不会终止任务,我必须从 shell 手动终止它。
因此,如果这不是正确的方法,我该如何允许任务正常关闭?
I am using celery 2.4.1 with python 2.6, the rabbitmq backend, and django. I would like my task to be able to clean up properly if the worker shuts down. As far as I am aware you cannot supply a task destructor so I tried hooking into the worker_shutdown signal.
Note: AbortableTask only works with the database backend so I cant use that.
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()
However, the shutdown hook never gets called. Ctrl-C'ing the worker doesnt kill the task and I have to manually kill it from the shell.
So if this is not the proper way to go about it, how do I allow tasks to shutdown gracefully?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
worker_shutdown
仅由MainProcess
发送,而不是由子池工作线程发送。除worker_process_init之外的所有
worker_*
信号,请参阅MainProcess
。在正常(热)关闭下,工作线程永远不会终止任务。
即使任务需要几天才能完成,工作人员也不会完成关闭
直到完成。您可以将
--soft-time-limit
或--time-limit
设置为告诉实例何时可以终止任务。
因此,要添加任何类型的进程清理过程,您首先需要
确保任务能够真正完成。因为清理不会
在此之前被调用。
要向池工作进程添加清理步骤,您可以使用
像这样的东西:
worker_shutdown
is only sent by theMainProcess
, not the child pool workers.All
worker_*
signalsexcept for worker_process_init
, refer to theMainProcess
.The worker never terminates a task under normal (warm) shutdown.
Even if a task takes days to complete, the worker won't complete shutdown
until it's completed. You can set
--soft-time-limit
, or--time-limit
toto tell the instance when it's ok to terminate the task.
So to add any kind of process cleanup process you first need to
make sure that the tasks can actually complete. As the cleanup wouldn't
be called before that happens.
To add a cleanup step to the pool worker processes you can use
something like:
使用worker_shutting_down信号。
请参阅 https://docs.celeryq.dev/en /stable/userguide/signals.html#worker-shutting-down 和 https://stackoverflow.com/a/55481656/237091 为例。
Use the worker_shutting_down signal.
See https://docs.celeryq.dev/en/stable/userguide/signals.html#worker-shutting-down and https://stackoverflow.com/a/55481656/237091 for an example.