由膳绫擎的例子可以看出,存储在LocalStack中的信息以字典的情势存在:键为线程/协程的标识数值,值也是字典情势。每当有一个线程/协程上要将一个对象push进LocalStack栈中,会形成如上一个“键-值”对。如许的一种构造很好地实现了线程/协程的隔离,每个线程/协程都邑根据本身线程/协程的标识数值肯定存储在栈构造中的值。
下面以一个多应用的例子进行解释:
LocalStack还实现了push、pop、top等办法。个中top办法永远指向栈顶的元素。栈顶的元素是指当前哨程/协程中最后被推入栈中的元素,即local_stack._local.stack[-1](留意,是stack键对应的对象中最后被推入的元素)。
请求高低文
Flask中所有的请求处理都在“请求高低文”中进行,在它设计之初便就有这个概念。因为0.9版本代码比较复杂,这里照样以0.1版本的代码为例进行解释。本质上这两个版本的“请求高低文”的运行道理没有变更,只是新版本增长了一些功能,这点在后面再进行解释。
请求高低文——0.1版本
- # Flask v0.1
- class _RequestContext(object):
- """The request context contains all request relevant information. It is
- created at the beginning of the request and pushed to the
- `_request_ctx_stack` and removed at the end of it. It will create the
- URL adapter and request object for the WSGI environment provided.
- """
- def __init__(self, app, environ):
- self.app = app
- self.url_adapter = app.url_map.bind_to_environ(environ)
- self.request = app.request_class(environ)
- self.session = app.open_session(self.request)
- self.g = _RequestGlobals()
- self.flashes = None
- def __enter__(self):
- _request_ctx_stack.push(self)
- def __exit__(self, exc_type, exc_value, tb):
- # do not pop the request stack if we are in debug mode
推荐阅读
2025年全球数据将攀升至163ZB,10倍的数据增长来源于哪?
【51CTO.com原创稿件】比来,希捷科技与IDC合营宣布了一份名为《数据时代2025》的数据成长趋势申报,这份白皮>>>详细阅读
本文标题:Flask中的请求上下文和应用上下文
地址:http://www.17bianji.com/lsqh/35165.html
1/2 1

网友点评
精彩导读
科技快报
品牌展示