Transfer of Flask Session,request,current_app
1 Flask request, session and Current_app are set up in a relatively new way-through the context of the management of the way to achieve
每次请求进来app.run调用 call 方法, 创建 一个本地线程(唯一标识作为键) -- 然后把实例化的对象push到一个地方,在请求结束后返回的时候 pop 掉local = { '标识':{'stack':[RequestContext(),]} }
2 Supplemental Partial function
其实就是函数调用的时候,有多个参数 参数,但是其中的一个参数已经知道了,我们可以通过这个参数重新绑定一个新的函数,然后去调用这个新函数。from functools import partialdef f(a,b): return a + bf1 = partial(f,10)print(f1(5))-->> 15
3 Unique identification
theading_local -->> 每一个线程 创建一个from greenlet import getcurrent as get_ident可以基于 greenlet -->> 粒度更细 比如 wsgi -- 有基于线程的,也有基于协程实现的本地线程: import threading local_values = threading.local() def func(num): local_values.name = num import time time.sleep(1) print(local_values.name, threading.current_thread().name) for i in range(20): th = threading.Thread(target=func, args=(i,), name='线程%s' % i) th.start()
Flask Context Management--(delivery of Session,request,current_app)