Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 317 Vote(s) - 3.56 Average
  • 1
  • 2
  • 3
  • 4
  • 5
uWSGI and gracefully killing a multithreaded Flask app

#1
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 !!!`
Reply

#2
>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")
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through