【问题标题】:Multiple Flask Application in single uwsgi单个 uwsgi 中的多个 Flask 应用程序
【发布时间】:2020-01-08 04:57:18
【问题描述】:

我有一个带有 uwsgi 配置的烧瓶应用程序。这个烧瓶过程请求诸如加法、减法和乘法。现在在我的项目结构中,我有一个应用程序,这个应用程序在 uwsgi 配置中被调用。但是现在我需要为每个操作分别设置烧瓶应用程序,即烧瓶 1 用于处理加法,烧瓶 2 用于处理减法等。我完全是一个初学者,不知道如何通过 uwsgi 实现这一点。

我听说过 uwsgi 皇帝模式,但不知道

我的应用文件:

from myapp import app

if __name__ == __main__:
  app.run()

wsgi 配置

module = wsgi:app

【问题讨论】:

    标签: python-3.x nginx flask uwsgi


    【解决方案1】:

    您可以使用 Werkzeug 的 Dispatcher Middleware 来做到这一点。

    使用这样的示例应用程序:

    # application.py
    
    from flask import Flask
    
    def create_app_root():
        app = Flask(__name__)
        @app.route('/')
        def index():
            return 'I am the root app'
        return app
    
    def create_app_1():
        app = Flask(__name__)
        @app.route('/')
        def index():
            return 'I am app 1'
        return app
    
    def create_app_2():
        app = Flask(__name__)
        @app.route('/')
        def index():
            return 'I am app 2'
        return app
    
    from werkzeug.middleware.dispatcher import DispatcherMiddleware
    
    dispatcher_app = DispatcherMiddleware(create_app_root(), {
        '/1': create_app_1(),
        '/2': create_app_2(),
    })
    

    然后你可以用 gunicorn 运行它:

    gunicorn --bind 0.0.0.0:5000 application:dispatcher_app
    

    并使用 curl 进行测试:

    $ curl -L http://localhost:5000/
    I am the root app%
    
    $ curl -L http://localhost:5000/1
    I am app 1%
    
    $ curl -L http://localhost:5000/2
    I am app 2%                    
    

    这似乎可以通过发出重定向来工作,这就是-L 标志的原因。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-03-19
      • 2016-09-29
      • 1970-01-01
      • 2014-01-21
      • 1970-01-01
      • 1970-01-01
      • 2016-01-12
      相关资源
      最近更新 更多