简体   繁体   中英

uWSGI and gracefully killing a multithreaded Flask app

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

I am trying to figure out a way to detect "app restart" so that I will be able to shut down APScheduler thread pool. I'm restarting by sending SIGHUP to uWSGI master process.

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

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

signal_wait function blocks so my threads run but uWSGI doesn't serve requests. I have also tried setting scheduler.daemonic to False and True - it doesn't help either way. The uWSGI process still logs something like this:

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

I am trying to figure out a way to detect "app restart" so that I will be able to shut down APScheduler thread pool.

I think there isn't simple way to certainly detect app restart , but uwsgi can execute code after reload or shut down, in these ways:

1) Code will execute in separate process: add hook-as-user-atexit to your uwsgi config:

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

2) Will be invoked 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 should reload thru touch uwsgi.pid . Will be invoked in one of the workers, only after reload:

[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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM