单个gunicon服务器提供服务的多个flask应用程序

时间:2019-09-16 11:45:48

标签: python flask gunicorn

我有一项服务,其中包含2个Flask应用程序-我们将其命名为 app monitoring_app 不同端口上运行。 monitoring_app 是一项实用程序服务,它提供通过prometheus_client收集的指标。我遇到了一个问题,尝试在Gunicorn服务器上正确运行它们。

我已经阅读了类似的主题: Multiple Flask Application in single uwsgi 但似乎我的问题无法用Dispatcher Middleware来解决。

我可以在没有Gunicorn的情况下启动这些应用程序,

from threading import Thread
import os
from my_project import create_app, create_monitoring_app


def start_app(my_app):
    my_app.run(host="localhost",
               debug=True,
               port=int(os.environ.get("API_PORT", "5000")),
               threaded=True,
               use_reloader=False)


if __name__ == "__main__":
    app = create_app()
    app_thread = Thread(target=start_app, daemon=True, args=(app,))
    app_thread.start()

    monitoring_app = create_monitoring_app()
    monitoring_app.run(host="localhost",
                       port=int(os.environ.get("OPS_PORT", "5001")),
                       threaded=True,
                       use_reloader=False,
                       debug=True)

它可以用于开发,但是可以在Flask开发服务器上运行,但不适用于生产环境。使用Gunicorn,我可以分别启动它们:

gunicorn "my_project:create_monitoring_app()" -b "[::]:$OPS_PORT" &
gunicorn "my_project:create_app()" -b "[::]:$API_PORT" 

但是他们将使用不同的口译员,并且我不能在 monitoring_app 中使用 app prometheus_client >

如何实现与从开发环境运行这些应用程序相同的行为?还是我做错了什么,应该以另一种方式做?

0 个答案:

没有答案