UWSGI and gracefully kills a multithreaded Flask application

I am implementing a system that uses APScheduler (which uses a thread pool) to get some resources.

I am trying to figure out a way to detect "application reload" so that I can close the APScheduler thread pool. I restart by sending SIGHUP to the uWSGI master process.

Has anyone tried one of them before? If so, what is the correct way to detect an application restart event?

  • uwsgidecorators has a postfork decorator,
  • uwsgi module has the functions signal_wait and signal_received

signal_wait , so my threads are executing, but uWSGI is not executing requests. I also tried setting scheduler.daemonic to False and True - this does not help anyway. The uWSGI process is still registering something like this:

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

+7
source share
1 answer

I am trying to figure out a way to detect "application reload" so that I can close the APScheduler thread pool.

I think there is no easy way for sure to detect a restart of the application, but uwsgi can execute the code after rebooting or closing, as follows:

1) The code will be executed in a separate process: add hook-as-user-atexit to the uwsgi configuration:

 [uwsgi] ... hook-as-user-atexit = exec:python finalization.py 

2) Will be called in one of the workers:

 import uwsgi def will_invoked_after_reload_or_shutdown(): print("I was invoked") uwsgi.atexit = will_invoked_after_reload_or_shutdown 

3) In this case, you must restart touch uwsgi.pid . It will be called by one of the workers, only after a reboot:

 [uwsgi] ... pidfile = ./uwsgi.pid touch-reload = ./uwsgi.pid 

Python Code:

 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") 
+1
source

All Articles