flask多app和栈的应用
2018-12-20 09:35:12来源:博客园 阅读 ()
一、简介
flask的蓝图可以实现url的分发,当有多个app时也可以利用app进行url分发,这里介绍下使用方式和内部原理以及栈的应用。
二、多app使用
使用示例
from werkzeug.wsgi import DispatcherMiddleware from werkzeug.serving import run_simple from flask import Flask app01 = Flask('app01') app02 = Flask('app02') @app01.route('/index') def index(): return "app01" @app02.route('/index') def index2(): return "app02" app = DispatcherMiddleware(app01, { '/app01': app01, '/app02': app02, }) #默认使用app01的路由,也就是访问 http://127.0.0.1:5000/index 返回app01 #当以app01开头时候使用app01的路由,也就是http://127.0.0.1:5000/app01/index 返回app01 #当以app02开头时候使用app02的路由,也就是http://127.0.0.1:5000/app02/index 返回app02 if __name__ == "__main__": run_simple('127.0.0.1', 5000, app)
实现原理
多app使用借助于DispatcherMiddleware,让我们看看其源码类的定义:
class DispatcherMiddleware(object): """Allows one to mount middlewares or applications in a WSGI application. This is useful if you want to combine multiple WSGI applications:: app = DispatcherMiddleware(app, { '/app2': app2, '/app3': app3 }) """ def __init__(self, app, mounts=None): self.app = app self.mounts = mounts or {} def __call__(self, environ, start_response): script = environ.get('PATH_INFO', '') path_info = '' while '/' in script: if script in self.mounts: app = self.mounts[script] break script, last_item = script.rsplit('/', 1) path_info = '/%s%s' % (last_item, path_info) else: app = self.mounts.get(script, self.app) original_script_name = environ.get('SCRIPT_NAME', '') environ['SCRIPT_NAME'] = original_script_name + script environ['PATH_INFO'] = path_info return app(environ, start_response)
def __call__(self, environ, start_response): script = environ.get('PATH_INFO', '') path_info = '' while '/' in script: if script in self.mounts: app = self.mounts[script] break script, last_item = script.rsplit('/', 1) path_info = '/%s%s' % (last_item, path_info) else: app = self.mounts.get(script, self.app) original_script_name = environ.get('SCRIPT_NAME', '') environ['SCRIPT_NAME'] = original_script_name + script environ['PATH_INFO'] = path_info return app(environ, start_response)
当script = /index是,while条件成立,同时对判断 /index是否在self.mounts,显然此时不在,分割/index,修改script为空,此时while不成立,执行app = self.mounts.get(script, self.app),返回self.app也就是app01,接着运行app01(),这也就是和在上下文中流程一样了。究其本质,就是通过url匹配出是哪个app,在运行该app的__call__方法。
三、栈是使用
flask中的请求数据存放实际利用列表构造的栈来存储的,每次pop都从最后pop出栈。当我们在进行测试或者写flask离线脚本时候可能会使用到上下文嵌套,例如:
from flask import Flask, current_app, _app_ctx_stack app1 = Flask('app01') app1.debug = False app2 = Flask('app02') app2.debug = True with app1.app_context(): print(_app_ctx_stack._local.__storage__) #{<greenlet.greenlet object at 0x10dc79e88>: {'stack': [<flask.ctx.AppContext object at 0x10dda7b00>]}} print(current_app.config['DEBUG']) # False with app2.app_context(): print(_app_ctx_stack._local.__storage__) #{<greenlet.greenlet object at 0x10dc79e88>: {'stack': [<flask.ctx.AppContext object at 0x10dda7b00>, <flask.ctx.AppContext object at 0x10dda7c18>]}} print(current_app.config['DEBUG']) # True print(current_app.config['DEBUG']) # False
在以上示例中在app01的上下文中嵌套了app02的上下文,所以在栈中会有两个app_ctx,但是在各自取上下文的时候都不会冲突,因为app02的上下文在最后,也就是第二个with中top是app02的app_ctx。
四、关于with
class Foo(object): def __init__(self,name): self.name=name def __enter__(self): print('run __enter__') return self.name def __exit__(self, exc_type, exc_val, exc_tb): print('run __exit__') with Foo('wd') as myname: print("vars:",myname) 结果: run __enter__ vars: wd run __exit__
可能你会注意在到,在以上的demo中__exit__方法中多了三个参数,即exc_type、exc_val、exc_tb这是对应着在with语句中代码出现异常时也会执行__exit__并接受异常,分别对应着:异常类型、异常值、以及异常的traceback。示例:
class Foo(object): def __init__(self,name): self.name=name def __enter__(self): print('run __enter__') return self.name def __exit__(self, exc_type, exc_val, exc_tb): print('run __exit__') print('exc_type:',exc_type) print('exc_val:',exc_val) print('exc_tb:',exc_tb) with Foo('wd') as myname: print("vars:", myname) a=[] v=a[1] print(v) 结果: run __enter__ vars: wd run __exit__ exc_type: <class 'IndexError'> exc_val: list index out of range exc_tb: <traceback object at 0x10ec98508> Traceback (most recent call last): File "dbapi.py", line 21, in <module> v=a[1] IndexError: list index out of range
最后我们回过头来看看app_context()对象中的__enter__方法和__exit__方法:
def __enter__(self): self.push() return self def __exit__(self, exc_type, exc_value, tb): self.pop(exc_value) if BROKEN_PYPY_CTXMGR_EXIT and exc_type is not None: reraise(exc_type, exc_value, tb)
实际上非常简单,执行__enter__时候调用push压栈,执行__exit__时pop出栈,这样就使得每个with里面都是当前app的上下文,而不会冲突。
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- python3 enum模块的应用 2019-08-13
- Flask request接口获取参数 2019-08-13
- 浅谈 Web框架 2019-08-13
- python 之 并发编程(生产者消费者模型、守护进程的应用) 2019-07-24
- Django源码安装xadmin报错Apps aren't loaded yet. 2019-07-24
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash