简体   繁体   中英

uWSGI + flask + own multiprocess?

I run a flask server with uWSGI. The Process is started by systemd.

Now I need some subprocesses that run with a "while(True)", they have to collect some information all the time.

I start at the moment a subprocess with multiprocessing.

Here some code from me:

from multiprocessing import Process, Value
def start_flask_server():
    daemon_data = Value('d', 0.0)
    p = Process(target=worker, args=(daemon_data, 1))
    p.daemon=True
    p.start()

Then I restart/stop now the uWSGI with systemd, the process blocks, course its not effected by the SIG.

My first idea was to implement a singal-handle:

for i in [x for x in dir(signal) if x.startswith("SIG")]:
        try:
            signum = getattr(signal,i)
            signal.signal(signum, signal_term_handler)
            app.logger.debug("Added Handler SIG: %s"%i)
        except Exception as e:
            app.logger.error(e)
            app.logger.error("Skipping %s"%i)

But unfortunately they dont fire up ...

Is there anyway to fire up an event if the server is going to be shutdown or can I start in my application an uWSGI "worker" to do the job?

I have two counter-suggestions for you:

  1. Create a separate systemd process for your worker. This works if your worker process doesn't need to communicate with your web processes, of it they can do so via some other coordinating process (for example: a database, Redis, Celery broker).

  2. Use a uwsgi mule https://uwsgi-docs.readthedocs.io/en/latest/Mules.html

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