Flask - 多APP应用(不太重要)
1. 多APP应用
请求进来时,可以根据URL的不同,交给不同的APP处理。一般用蓝图也可以实现。一般不写多app应用。
from werkzeug.wsgi import DispatcherMiddleware
from werkzeug.serving import run_simple
from flask import Flask, current_app
app1 = Flask('app01')
app2 = Flask('app02')
@app1.route('/index')
def index():
return "app01"
@app2.route('/index2')
def index2():
return "app2"
# localhost/index -- http://www.baidu.com/index
# localhost/sec/index2 -- http://www.baidu.com/sec/index2
dm = DispatcherMiddleware(app1, {
'/sec': app2,
})
if __name__ == "__main__":
app2.__call__
run_simple('localhost', 5000, dm)
看DispatcherMiddleware的源码,看app2.__call__源码
原理其实就是URL分割,然后请求分发给指定的app。然后请求分配给制定的app,之后app也按单app的流程走。就是从app.__call__走。