问题
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