uWSGI并优雅地杀死多线程Flask应用程序

sed*_*rek 8 python flask uwsgi

我正在实现一个使用APScheduler(使用线程池)的系统来获取一些资源.

我试图找出一种方法来检测"应用程序重启",以便我可以关闭APScheduler线程池.我正在重新启动,将SIGHUP发送到uWSGI主进程.

有没有人尝过其中之一?如果是这样,检测应用重启事件的正确方法是什么?

  • uwsgidecoratorspostfork装饰,
  • uwsgi模块signal_waitsignal_received功能

signal_wait功能块,所以我的线程运行,但uWSGI不提供请求.我也试过设置scheduler.daemonic为False和True - 它无论如何都没有用.uWSGI进程仍然记录如下:

worker 1 (pid: 20082) is taking too much time to die...NO MERCY !!!

Gre*_*eev 1

我正在尝试找出一种方法来检测“应用程序重新启动”,以便我能够关闭 APScheduler 线程池。

我认为没有简单的方法可以确定检测应用程序重新启动,但 uwsgi 可以通过以下方式在重新加载或关闭后执行代码:

1)代码将在单独的进程中执行:将hook-as-user-atexit添加到您的 uwsgi 配置中:

[uwsgi]
 ...
 hook-as-user-atexit = exec:python finalization.py
Run Code Online (Sandbox Code Playgroud)

2)将在其中一个工人中调用:

import uwsgi

def will_invoked_after_reload_or_shutdown():
    print("I was invoked")

uwsgi.atexit = will_invoked_after_reload_or_shutdown
Run Code Online (Sandbox Code Playgroud)

3)在这种情况下,您应该通过重新加载touch uwsgi.pid。仅在重新加载后才会在其中一个工作人员中调用:

[uwsgi]
 ...
 pidfile = ./uwsgi.pid
 touch-reload = ./uwsgi.pid
Run Code Online (Sandbox Code Playgroud)

Python代码:

import uwsgi

def will_executed_after_reload(*args):
    print("I was invoked")

uwsgi.register_signal(17, "worker", will_executed_after_reload)
uwsgi.add_file_monitor(17, "./uwsgi.pid")
Run Code Online (Sandbox Code Playgroud)