Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 !!!

like image 701
keremulutas Avatar asked Jan 08 '13 23:01

keremulutas


1 Answers

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")
like image 162
Greg Eremeev Avatar answered Oct 24 '22 00:10

Greg Eremeev