flask上下文管理
非原创
转载于:https://www.cnblogs.com/zhaopanpan/p/9457343.html
前引
在了解flask上下文管理机制之前,先来一波必知必会的知识点。
面向对象双下方法
首先,先来聊一聊面向对象中的一些特殊的双下划线方法,比如__call__、__getattr__系列、__getitem__系列。
__call__
这个方法相信大家并不陌生,在单例模式中,我们可能用到过,除此之外,还想就没有在什么特殊场景中用到了。我们往往忽视了它一个很特殊的用法:对象object+()或者类Foo()+()这种很特殊的用法。在Flask上下文管理中,入口就是使用了这种方式。
__getitem__系列
使用这个系列的方法时,我们最大的印象就是调用对象的属性可以像字典取值一样使用中括号([])。使用中括号对对象中的属性进行取值、赋值或者删除时,会自动触发对应的__getitem__、__setitem__、__delitem__方法。
class Foo(object):</span><span style="color: rgba(0, 0, 255, 1)">def</span> <span style="color: rgba(128, 0, 128, 1)">__init__</span><span style="color: rgba(0, 0, 0, 1)">(self): self.name </span>= <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">boo</span><span style="color: rgba(128, 0, 0, 1)">"</span> <span style="color: rgba(0, 0, 255, 1)">def</span> <span style="color: rgba(128, 0, 128, 1)">__getitem__</span><span style="color: rgba(0, 0, 0, 1)">(self, item): </span><span style="color: rgba(0, 0, 255, 1)">print</span>(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">调用__getitem__了</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">) </span><span style="color: rgba(0, 0, 255, 1)">if</span> item <span style="color: rgba(0, 0, 255, 1)">in</span> self.<span style="color: rgba(128, 0, 128, 1)">__dict__</span><span style="color: rgba(0, 0, 0, 1)">: </span><span style="color: rgba(0, 0, 255, 1)">return</span> self.<span style="color: rgba(128, 0, 128, 1)">__dict__</span><span style="color: rgba(0, 0, 0, 1)">[item] </span><span style="color: rgba(0, 0, 255, 1)">def</span> <span style="color: rgba(128, 0, 128, 1)">__setitem__</span><span style="color: rgba(0, 0, 0, 1)">(self, key, value): </span><span style="color: rgba(0, 0, 255, 1)">print</span>(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">调用__setitem__方法了</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">) self.</span><span style="color: rgba(128, 0, 128, 1)">__dict__</span>[key] =<span style="color: rgba(0, 0, 0, 1)"> value </span><span style="color: rgba(0, 0, 255, 1)">def</span> <span style="color: rgba(128, 0, 128, 1)">__delitem__</span><span style="color: rgba(0, 0, 0, 1)">(self, key): </span><span style="color: rgba(0, 0, 255, 1)">print</span>(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">调用__delitem__</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">) </span><span style="color: rgba(0, 0, 255, 1)">del</span> self.<span style="color: rgba(128, 0, 128, 1)">__dict__</span><span style="color: rgba(0, 0, 0, 1)">[key]foo = Foo()
ret = foo["name"]
# print(ret) # 输出 调用__getitem__了 boo
foo["age"] = 18
# print(foo["age"]) # 输出 调用__setitem__方法了 调用__getitem__了 18
del foo["age"] # 输出 调用__delitem__
__getattr__系列
使用对象取值、赋值或者删除时,会默认的调用对应的__getattr__、__setattr__、__delattr__方法。
对象取值时,取值的顺序为:先从__getattribute__中找,第二步从对象的属性中找,第三步从当前类中找,第四步从父类中找,第五步从__getattr__中找,如果没有,直接抛出异常。
class Foo(object):</span><span style="color: rgba(0, 0, 255, 1)">def</span> <span style="color: rgba(128, 0, 128, 1)">__init__</span><span style="color: rgba(0, 0, 0, 1)">(self): self.name </span>= <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">boo</span><span style="color: rgba(128, 0, 0, 1)">"</span> <span style="color: rgba(0, 0, 255, 1)">def</span> <span style="color: rgba(128, 0, 128, 1)">__getattr__</span><span style="color: rgba(0, 0, 0, 1)">(self, item): </span><span style="color: rgba(0, 0, 255, 1)">print</span>(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">调用__getattr__了</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">) </span><span style="color: rgba(0, 0, 255, 1)">def</span> <span style="color: rgba(128, 0, 128, 1)">__setattr__</span><span style="color: rgba(0, 0, 0, 1)">(self, key, value): </span><span style="color: rgba(0, 0, 255, 1)">print</span>(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">调用__setattr__方法了</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">) </span><span style="color: rgba(0, 0, 255, 1)">def</span> <span style="color: rgba(128, 0, 128, 1)">__delattr__</span><span style="color: rgba(0, 0, 0, 1)">(self, item): </span><span style="color: rgba(0, 0, 255, 1)">print</span>(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">调用__delattr__</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)foo = Foo()
ret = foo.xxx # 输出 调用__getattr__了
foo.age = 18 # 调用__setattr__方法了
del foo.age # 输出 调用__delattr__
偏函数
再来说说Python中的偏函数
python中有一个小工具包functools,这个包中包含了几个在很有用的小功能,比如:wraps:在使用装饰器时,使用这个方法可以保护函数的元信息。reduce:一个合并序列项为一个单一值的小方法。还有一个就是偏函数: partial
一句话来总结partial的作用,固定函数中的一些参数,返回一个新的函数,方便调用
from functools import partialclass Foo(object):
</span><span style="color: rgba(0, 0, 255, 1)">def</span> <span style="color: rgba(128, 0, 128, 1)">__init__</span><span style="color: rgba(0, 0, 0, 1)">(self): self.request </span>= <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">request</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)"> self.session </span>= <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">session</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">foo = Foo()
def func(args):
return getattr(foo,args)re_func = partial(func,'request')
se_func = partial(func,'session')print(re_func())
仿照flask来实现一个更复杂的
from functools import partialclass HttpRequest(object):
</span><span style="color: rgba(0, 0, 255, 1)">def</span> <span style="color: rgba(128, 0, 128, 1)">__init__</span><span style="color: rgba(0, 0, 0, 1)">(self): self.method </span>= <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">GET</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)"> self.body </span>= b<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">name=abc@age=123</span><span style="color: rgba(128, 0, 0, 1)">"</span>class Foo(object):
</span><span style="color: rgba(0, 0, 255, 1)">def</span> <span style="color: rgba(128, 0, 128, 1)">__init__</span><span style="color: rgba(0, 0, 0, 1)">(self): self.request </span>=<span style="color: rgba(0, 0, 0, 1)"> HttpRequest() self.session </span>= {<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">login</span><span style="color: rgba(128, 0, 0, 1)">"</span>:True,<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">is_super</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">:False}foo = Foo()
def func(args):
return getattr(foo,args)re_func = partial(func,'request')
se_func = partial(func,'session')class LocalProxy(object):
</span><span style="color: rgba(0, 0, 255, 1)">def</span> <span style="color: rgba(128, 0, 128, 1)">__init__</span><span style="color: rgba(0, 0, 0, 1)">(self,local): self._local </span>=<span style="color: rgba(0, 0, 0, 1)"> local </span><span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> _get_current_object(self): </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> self._local() </span><span style="color: rgba(0, 0, 255, 1)">def</span> <span style="color: rgba(128, 0, 128, 1)">__getitem__</span><span style="color: rgba(0, 0, 0, 1)">(self, item): </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> getattr(self._get_current_object(),item)request = LocalProxy(re_func)
ret = request._get_current_object().method
print(ret)ret = request['method']
print(ret)session = LocalProxy(se_func)
print(session._get_current_object())
threading.local
再来说一说threading.local方法
在多线程中,同一个进程中的多个线程是共享一个内存地址的,多个线程操作数据时,就会造成数据的不安全,所以我们就要加锁。但是,对于一些变量,如果仅仅只在本线程中使用,怎么办?
方法一,可以通过全局的字典,key为当前线程的线程ID,value为具体的值。
方法二,使用threading.local方法
threading.local 在多线程操作时,为每一个线程创建一个值,使得线程之间各自操作自己 的值,互不影响。
import time import threadinglocal = threading.local()
def func(n):
local.val = n
time.sleep(5)
print(n)for i in range(10):
t = threading.Thread(target=func,args=(i,))
t.start()# 结果输出 0--9
自定义使用threading.local的功能
import time import threading # from threading import current_thread as getcurrent from greenlet import getcurrentclass Local(object):
</span><span style="color: rgba(0, 0, 255, 1)">def</span> <span style="color: rgba(128, 0, 128, 1)">__init__</span><span style="color: rgba(0, 0, 0, 1)">(self): object.</span><span style="color: rgba(128, 0, 128, 1)">__setattr__</span>(self,<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">_storage</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">,{}) </span><span style="color: rgba(0, 0, 255, 1)">def</span> <span style="color: rgba(128, 0, 128, 1)">__setattr__</span><span style="color: rgba(0, 0, 0, 1)">(self, key, value): </span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> ident = threading.get_ident()</span> ident = getcurrent() <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 定制粒度更细的</span> <span style="color: rgba(0, 0, 255, 1)">if</span> ident <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> self._storage: self._storage[ident][key] </span>=<span style="color: rgba(0, 0, 0, 1)"> value </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">: self._storage[ident] </span>=<span style="color: rgba(0, 0, 0, 1)"> {key:value} </span><span style="color: rgba(0, 0, 255, 1)">def</span> <span style="color: rgba(128, 0, 128, 1)">__getattr__</span><span style="color: rgba(0, 0, 0, 1)">(self, item): </span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> ident = threading.get_ident()</span> ident =<span style="color: rgba(0, 0, 0, 1)"> getcurrent() </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> self._storage[ident][item]local = Local()
def func(n):
local.val = n
time.sleep(2)
print(local.val)for i in range(10):
t = threading.Thread(target=func,args=(i,))
t.start()
仿照flask用栈来实现自定义threading.local的存取
from greenlet import getcurrentclass Local(object):
</span><span style="color: rgba(0, 0, 255, 1)">def</span> <span style="color: rgba(128, 0, 128, 1)">__init__</span><span style="color: rgba(0, 0, 0, 1)">(self): object.</span><span style="color: rgba(128, 0, 128, 1)">__setattr__</span>(self,<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">_storage</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">,{}) </span><span style="color: rgba(0, 0, 255, 1)">def</span> <span style="color: rgba(128, 0, 128, 1)">__setattr__</span><span style="color: rgba(0, 0, 0, 1)">(self, key, value): </span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> ident = threading.get_ident()</span> ident = getcurrent() <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 定制粒度更细的</span> <span style="color: rgba(0, 0, 255, 1)">if</span> ident <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> self._storage: self._storage[ident][key] </span>=<span style="color: rgba(0, 0, 0, 1)"> value </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">: self._storage[ident] </span>=<span style="color: rgba(0, 0, 0, 1)"> {key:value} </span><span style="color: rgba(0, 0, 255, 1)">def</span> <span style="color: rgba(128, 0, 128, 1)">__getattr__</span><span style="color: rgba(0, 0, 0, 1)">(self, item): </span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> ident = threading.get_ident()</span> ident =<span style="color: rgba(0, 0, 0, 1)"> getcurrent() </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> self._storage[ident][item]class LocalStack(object):
</span><span style="color: rgba(0, 0, 255, 1)">def</span> <span style="color: rgba(128, 0, 128, 1)">__init__</span><span style="color: rgba(0, 0, 0, 1)">(self): self.local </span>=<span style="color: rgba(0, 0, 0, 1)"> Local() </span><span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> push(self,item): self.local.stack </span>=<span style="color: rgba(0, 0, 0, 1)"> [] self.local.stack.append(item) </span><span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> pop(self): </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> self.local.stack.pop() </span><span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> top(self): </span><span style="color: rgba(0, 0, 255, 1)">return</span> self.local.stack[-1<span style="color: rgba(0, 0, 0, 1)">]_local_stack = LocalStack()
_local_stack.push(55)
print(_local_stack.top()) # 取栈顶元素
预热完毕,来一波真正的操作,不过在正戏上演之前,先来提一嘴,flask与其他python框架比如(Django、tornado)在整个请求生命周期中对于数据的管理机制的不同。django、tornado是通过传参的形式传递数据,而flask是通过其特有的上下文管理机制来管理数据的。
下面进入我们今天的正题----flask的上下文管理机制。在flask中,上下文管理机制分为两个大的部分:请求上下文和应用上下文。
flask的上下文管理机制
接下来,从以下三个大的方面分别探讨flask的两大上下文管理机制。
- 方面一:请求进来时
- 方面二:视图函数
- 方面三:请求结束前
先来一个最简单的flask版的Hello World
from flask import Flaskapp = Flask(name)
@app.route('/')
def index():
return "Hello World"if name == 'main':
app.run()
启动一个flask项目时,会先执行app.run()方法,这是整个项目的入口,执行run方法时,接着执行werkzeug模块中的run_simple

werkzeug中触发调用了Flask的__call__方法
请求进来时
触发执行__call__方法,__call__方法的逻辑很简单,直接执行wsgi_app方法,将包含所有请求相关数据和一个响应函数传进去。

def application(environ, start_response): start_response('200 OK', [('Content-Type', 'text/html')]) return '<h1>Hello, web!</h1>'上面的application()函数就是符合WSGI标准的一个HTTP处理函数,它接收两个参数:
environ:一个包含所有HTTP请求信息的dict对象; start_response:一个发送HTTP响应的函数。在application()函数中,调用:
start_response('200 OK', [('Content-Type', 'text/html')])
备注:__call__是一个符合wsgi标准的函数
执行wsgi_app方法
def wsgi_app(self, environ, start_response):ctx </span>=<span style="color: rgba(0, 0, 0, 1)"> self.request_context(environ) error </span>=<span style="color: rgba(0, 0, 0, 1)"> None </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)">: </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)">: ctx.push() response </span>=<span style="color: rgba(0, 0, 0, 1)"> self.full_dispatch_request() </span><span style="color: rgba(0, 0, 255, 1)">except</span><span style="color: rgba(0, 0, 0, 1)"> Exception as e: error </span>=<span style="color: rgba(0, 0, 0, 1)"> e response </span>=<span style="color: rgba(0, 0, 0, 1)"> self.handle_exception(e) </span><span style="color: rgba(0, 0, 255, 1)">except</span><span style="color: rgba(0, 0, 0, 1)">: error </span>= sys.exc_info()[1<span style="color: rgba(0, 0, 0, 1)">] </span><span style="color: rgba(0, 0, 255, 1)">raise</span> <span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> response(environ, start_response) </span><span style="color: rgba(0, 0, 255, 1)">finally</span><span style="color: rgba(0, 0, 0, 1)">: </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> self.should_ignore_error(error): error </span>=<span style="color: rgba(0, 0, 0, 1)"> None ctx.auto_pop(error)</span></pre>
第一步先执行了一个request_context的方法,将environ传进去,最后返回一个RequestContext类的对象,被ctx的变量接收(ctx=request_context(environ))
def request_context(self, environ): """Create a :class:`~flask.ctx.RequestContext` representing a WSGI environment. Use a ``with`` block to push the context, which will make :data:`request` point at this request.See :doc:`/reqcontext`. Typically you should not call this from your own code. A request context is automatically pushed by the :meth:`wsgi_app` when handling a request. Use :meth:`test_request_context` to create an environment and context instead of this method. :param environ: a WSGI environment </span><span style="color: rgba(128, 0, 0, 1)">"""</span> <span style="color: rgba(0, 0, 255, 1)">return</span> RequestContext(self, environ)</pre>
这个ctx对象在初始化时,赋了两个非常有用的属性,一个是request,一个是session
def __init__(self, app, environ, request=None):
self.app = app
if request is None:
request = app.request_class(environ)
self.request = request
self.url_adapter = app.create_url_adapter(self.request)
self.flashes = None
self.session = None
这两个属性中request是一个Request()对象,这个对象就是我们在flask中使用的request对象,为我们提供了很多便捷的属性和方法,比如:request.method、request.form、request.args等等,另一个属性是session,初始为None。
紧接着执行ctx.push()方法,这个方法中,在执行请求上下文对象ctx之前先实例化了一个app_context对象,先执行了app_context的push方法,然后才执行_request_ctx_stack对象中的top和_request_ctx_stack.push(self),最后对ctx中的session进行处理。
所以,flask中的应用上下文发生在请求上下文之前。
def push(self):top </span>=<span style="color: rgba(0, 0, 0, 1)"> _request_ctx_stack.top </span><span style="color: rgba(0, 0, 255, 1)">if</span> top <span style="color: rgba(0, 0, 255, 1)">is</span> <span style="color: rgba(0, 0, 255, 1)">not</span> None <span style="color: rgba(0, 0, 255, 1)">and</span><span style="color: rgba(0, 0, 0, 1)"> top.preserved: top.pop(top._preserved_exc) </span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 在执行request_context请求上下文的push方法时,先执行了app_context应用上下文的push方法</span> app_ctx =<span style="color: rgba(0, 0, 0, 1)"> _app_ctx_stack.top </span><span style="color: rgba(0, 0, 255, 1)">if</span> app_ctx <span style="color: rgba(0, 0, 255, 1)">is</span> None <span style="color: rgba(0, 0, 255, 1)">or</span> app_ctx.app !=<span style="color: rgba(0, 0, 0, 1)"> self.app: app_ctx </span>=<span style="color: rgba(0, 0, 0, 1)"> self.app.app_context() app_ctx.push() self._implicit_app_ctx_stack.append(app_ctx) </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">: self._implicit_app_ctx_stack.append(None) </span><span style="color: rgba(0, 0, 255, 1)">if</span> hasattr(sys, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">exc_clear</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">): sys.exc_clear() </span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 然后执行请求上下文对象中LocalStack对象的push方法</span>_request_ctx_stack.push(self)
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 最后处理session</span> <span style="color: rgba(0, 0, 255, 1)">if</span> self.session <span style="color: rgba(0, 0, 255, 1)">is</span><span style="color: rgba(0, 0, 0, 1)"> None: session_interface </span>=<span style="color: rgba(0, 0, 0, 1)"> self.app.session_interface self.session </span>=<span style="color: rgba(0, 0, 0, 1)"> session_interface.open_session( self.app, self.request ) </span><span style="color: rgba(0, 0, 255, 1)">if</span> self.session <span style="color: rgba(0, 0, 255, 1)">is</span><span style="color: rgba(0, 0, 0, 1)"> None: self.session </span>= session_interface.make_null_session(self.app)</pre>
但是我们先说请求上下文,在处理完应用上下文的push方法后,紧接着执行了_request_ctx_stack对象的两个方法。
而这个_request_ctx_stack是LocalStack这个类的对象。_request_ctx_stack = LocalStack()
LocalStack有没有很眼熟,没错,flask内部使用的机制就是类似于我们上文中自定义的LocalStack的机制,实例化过程中使用了面向对象中的组合概念,self._local = Local(),然后在自身又实现了push、pop、top方法,这三个方法中都是通过反射从Local类的实例化对象中对一个stack属性进行append、pop、[-1]的操作,所以,Local对象中的stack属性对应的值一定是一个类似于列表的东西。通过对列表的操作,实现一个类似于栈的存取。
接着聊聊这个Local类,在实例化时,会对每个对象生成一个storage的空字典。我们翻遍整个Local类的源码,发现内部并没有实现一个叫stack的方法或者属性,但是上面我们提到了LocalStack对象会对Local对象中的一个叫stack的东西进行一系列操作。找不到不会报错吗?
这就是flask的巧妙之处,通过类的一些魔法方法巧妙的实现了相应的处理。在前引中,提到如果对象中没有某个属性,取值时,最终会执行类中的__getattr__方法,然后再做后续的异常处理,flask将所有的对应逻辑都实现在了类的__getattr__方法中,将每一个线程存储到字典中,在请求进来时,将每一个对应的请求ctx存在一个列表中,使用时直接调用,而不是通过传参的形式,更体现出了flask框架的轻量级。
处理完_request_ctx_stack后,就该处理session了。
在flask中,处理session时,非常的巧妙,完美的遵循了开闭原则,会先执行session_interface对象的open_session方法,在这个方法中,会先从用户请求的cookie中获取sessionid,获取该用户之前设置的session值,然后将值赋值到ctx.session中。
处理完session后,ctx.push方法就执行完了,返回到最开始的app.wsgi_app方法中,执行完push方法后,接着执行full_dispatch_request方法,从这个名字中我们也能猜到,这个方法只要是负责请求的分发。
def full_dispatch_request(self):self.try_trigger_before_first_request_functions() </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)">: request_started.send(self) rv </span>=<span style="color: rgba(0, 0, 0, 1)"> self.preprocess_request() </span><span style="color: rgba(0, 0, 255, 1)">if</span> rv <span style="color: rgba(0, 0, 255, 1)">is</span><span style="color: rgba(0, 0, 0, 1)"> None: rv </span>=<span style="color: rgba(0, 0, 0, 1)"> self.dispatch_request() </span><span style="color: rgba(0, 0, 255, 1)">except</span><span style="color: rgba(0, 0, 0, 1)"> Exception as e: rv </span>=<span style="color: rgba(0, 0, 0, 1)"> self.handle_user_exception(e) </span><span style="color: rgba(0, 0, 255, 1)">return</span> self.finalize_request(rv) </pre>
在full_dispatch_request方法中先执行preprocess_request方法,这个方法,会先执行所有被before_request装饰器装饰的函数,然后就通过路由的分发执行视图函数了(dispatch_request)
执行视图函数时
在执行视图函数之前,先执行了before_request,在执行我们的视图函数。
视图函数主要处理业务逻辑。在视图函数中可以调用request对象,进行取值,也可以调用session对象对session的存取。
在整个request的请求生命周期中,获取请求的数据直接调用request即可,对session进行操作直接调用session即可。request和session都是LocalProxy对象,借助偏函数的概念将对应的值传入_lookup_req_object函数。先从_request_ctx_stack(LocalStack)对象中获取ctx(请求上下文对象),再通过反射分别获取request和session属性。整个过程中LocalStack扮演了一个全局仓库的角色,请求进来将数据存取,需要时即去即用。所以,flask实现了在整个请求的生命周期中哪儿需要就直接调用的特色。
request = LocalProxy(partial(_lookup_req_object, 'request')) session = LocalProxy(partial(_lookup_req_object, 'session'))
请求结束前
视图函数执行完后,dispatch_request执行结束,执行full_dispatch_request方法的返回值finalize_request方法。这个方法中,同样的,在返回响应之前,先执行所有被after_request装饰器装饰的函数。
---->finalize_request ------> process_response
def process_response(self, response):ctx </span>=<span style="color: rgba(0, 0, 0, 1)"> _request_ctx_stack.top bp </span>=<span style="color: rgba(0, 0, 0, 1)"> ctx.request.blueprint funcs </span>=<span style="color: rgba(0, 0, 0, 1)"> ctx._after_request_functions </span><span style="color: rgba(0, 0, 255, 1)">if</span> bp <span style="color: rgba(0, 0, 255, 1)">is</span> <span style="color: rgba(0, 0, 255, 1)">not</span> None <span style="color: rgba(0, 0, 255, 1)">and</span> bp <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> self.after_request_funcs: funcs </span>=<span style="color: rgba(0, 0, 0, 1)"> chain(funcs, reversed(self.after_request_funcs[bp])) </span><span style="color: rgba(0, 0, 255, 1)">if</span> None <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> self.after_request_funcs: funcs </span>=<span style="color: rgba(0, 0, 0, 1)"> chain(funcs, reversed(self.after_request_funcs[None])) </span><span style="color: rgba(0, 0, 255, 1)">for</span> handler <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> funcs: response </span>=<span style="color: rgba(0, 0, 0, 1)"> handler(response) </span><span style="color: rgba(0, 0, 255, 1)">if</span> <span style="color: rgba(0, 0, 255, 1)">not</span><span style="color: rgba(0, 0, 0, 1)"> self.session_interface.is_null_session(ctx.session): self.session_interface.save_session(self, ctx.session, response) </span><span style="color: rgba(0, 0, 255, 1)">return</span> response</pre>
执行process_response过程中,执行完after_request后,然后,执行session的save_session方法。将内存中保存在ctx.session的值取到后,json.dumps()序列化后,写入响应的cookie中(set_cookie),最后返回响应。
def save_session(self, app, session, response): domain = self.get_cookie_domain(app) path = self.get_cookie_path(app)</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> If the session is modified to be empty, remove the cookie.</span> <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> If the session is empty, return without setting the cookie.</span> <span style="color: rgba(0, 0, 255, 1)">if</span> <span style="color: rgba(0, 0, 255, 1)">not</span><span style="color: rgba(0, 0, 0, 1)"> session: </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> session.modified: response.delete_cookie( app.session_cookie_name, domain</span>=<span style="color: rgba(0, 0, 0, 1)">domain, path</span>=<span style="color: rgba(0, 0, 0, 1)">path ) </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> Add a "Vary: Cookie" header if the session was accessed at all.</span> <span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> session.accessed: response.vary.add(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Cookie</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">) </span><span style="color: rgba(0, 0, 255, 1)">if</span> <span style="color: rgba(0, 0, 255, 1)">not</span><span style="color: rgba(0, 0, 0, 1)"> self.should_set_cookie(app, session): </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> httponly </span>=<span style="color: rgba(0, 0, 0, 1)"> self.get_cookie_httponly(app) secure </span>=<span style="color: rgba(0, 0, 0, 1)"> self.get_cookie_secure(app) samesite </span>=<span style="color: rgba(0, 0, 0, 1)"> self.get_cookie_samesite(app) expires </span>=<span style="color: rgba(0, 0, 0, 1)"> self.get_expiration_time(app, session) val </span>=<span style="color: rgba(0, 0, 0, 1)"> self.get_signing_serializer(app).dumps(dict(session)) </span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> set_cookie将session写入响应的cookie中</span>response.set_cookie(
app.session_cookie_name,
val,
expires=expires,
httponly=httponly,
domain=domain,
path=path,
secure=secure,
samesite=samesite
)
返回响应后,自动的调用ctx.auto_pop(error),将Local中存储的ctx对象pop掉,整个请求结束。
请求上下文的执行流程:

应用上下文
与请求上下文类似,当请求进来时,先实例化一个AppContext对象app_ctx,在实例化的过程中,提供了两个有用的属性,一个是app,一个是g。self.app就是传入的全局的app对象,self.g是一个全局的存储值的对象。接着将这个app_ctx存放到LocalStack()。
class AppContext(object):
def __init__(self, app):
self.app = app
self.url_adapter = app.create_url_adapter(None)
self.g = app.app_ctx_globals_class()
视图函数中,我们就可以调用app对象和g对象,如果我们使用蓝图构建我们的项目时,在每一个直接引用app就会造成循环引用的异常,这时,应用上下文就会显得非常有用,我们可以直接调用current_app就可以在整个生命周期中使用我们的app对象了。比如使用我们的配置项:current_app.config
current_app = LocalProxy(_find_app)
最后,当视图函数执行结束后,从storage中pop掉app_ctx对象。
from flask import Flask,request,session,gapp = Flask(name) # type:Flask
@app.before_request
def auth_demo():
g.val = 123@app.route('/')
def index():
print(g.val)
return "Hello World"if name == 'main':
app.run()
总结:flask中的上下文管理机制分为请求上下文和应用上下文两大方面,通过上下文的管理机制,实现了即去即用的一个特色。
作者:赵盼盼
出处:https://www.cnblogs.com/zhaopanpan/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
浙公网安备 33010602011771号