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
functionssignal_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")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With