Multiple Flask Application in single uwsgi

こ雲淡風輕ζ 提交于 2019-12-25 01:36:00

问题


I have a flask application with uwsgi configuration. This flask process requests such as addition, subtraction and multiplication. Now in my project structure i have a single app and this app is called in uwsgi config. But now i need to have separate flask application for each operation i.e flask1 for processing addition and flask2 for processing subtraction and so on. I am totally a beginner and have no idea how to achieve this through uwsgi.

I have heard about uwsgi emperor mode but doesn' have idea on it

My app file :

from myapp import app

if __name__ == __main__:
  app.run()

wsgi config

module = wsgi:app

回答1:


You could do this by using Werkzeug's Dispatcher Middleware.

With a sample application like this:

# 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(),
})

You can then run this with gunicorn:

gunicorn --bind 0.0.0.0:5000 application:dispatcher_app

And test with 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%                    

This seems to work by issuing a redirect, which is the reason for the -L flag here.



来源:https://stackoverflow.com/questions/57802947/multiple-flask-application-in-single-uwsgi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!