简体   繁体   English

uWSGI + Flask + 自己的多进程?

[英]uWSGI + flask + own multiprocess?

I run a flask server with uWSGI.我用 uWSGI 运行了一个 Flask 服务器。 The Process is started by systemd.该进程由 systemd 启动。

Now I need some subprocesses that run with a "while(True)", they have to collect some information all the time.现在我需要一些以“while(True)”运行的子进程,它们必须一直收集一些信息。

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.然后我现在用 systemd 重新启动/停止 uWSGI,进程阻塞,当然它不受 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?无论如何,如果服务器将要关闭,是否可以启动一个事件,或者我可以在我的应用程序中启动一个 uWSGI“工人”来完成这项工作?

I have two counter-suggestions for you:我有两个反建议给你:

  1. Create a separate systemd process for your worker.为您的工作人员创建一个单独的 systemd 进程。 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).如果您的工作进程不需要与您的 Web 进程通信,那么这会起作用,其中它们可以通过其他一些协调进程(例如:数据库、Redis、Celery 代理)来进行。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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