一篇引用大牛的
https://www.cnblogs.com/zhaopanpan/p/9457343.html
### 线程安全  

```python
# 线程不安全
class Foo(object):
pass foo = Foo()
foo.num = 1 import time
import threading def my_num(i):
foo.num = i
time.sleep(1)
print(foo.num,threading.current_thread().ident) for i in range(20):
th = threading.Thread(target=my_num,args=(i,))
th.start()
---------------------------------------------------------------------------- # 线程安全
from threading import local
class Foo(local):
pass foo = Foo()
foo.num = 1 import time
import threading def my_num(i):
foo.num = i
time.sleep(1)
print(foo.num,threading.current_thread().ident) for i in range(200000):
th = threading.Thread(target=my_num,args=(i,))
th.start() # 开辟新空间 - 浪费了部分资源 - 速度保证 - 保证安全
"""
{
16232:[request],# 123
17164:[request],# 456
9088: [request],# 567
16084:[request],# 789
} Threading.local
Flask 上下文机制就是使用的 Threading.local
线程进来 开辟一个空间给这个线程,线程操作的所有任务,都会复制一份儿到空间中
"""
``` ### 栈 Stack ```python
# Stack
class MyStack(object):
data = [] def __setattr__(self, key, value):
self.push(key,value) def push(self,key,value):
self.data.append({key:value}) def top(self):
return self.data.pop() my_stack = MyStack() my_stack.name = 666 # [{name:666}]
my_stack.name = 777 # [{name:666},{name:777}] print(my_stack.data) # my_stack.push(666)
# my_stack.push(777)
# my_stack.push(888)
#
# print(my_stack.top(),my_stack.data)
# print(my_stack.top(),my_stack.data)
# print(my_stack.top(),my_stack.data)
``` ### 局部的栈 LocalStack ```python
# LocalStack 安全的 栈
from threading import get_ident # 获取当前线程的 id
import threading class MyLocalStack(object):
storage={}
def push(self,item):
try:
self.storage[get_ident()].append(item)
except:
self.storage[get_ident()]=[item] def top(self):
return self.storage[get_ident()].pop() my_local_stack = MyLocalStack() import time
def go(i):
my_local_stack.push(i)
time.sleep(1)
# my_local_stack.top() for i in range(5):
th = threading.Thread(target=go,args=(i,))
th.start() print(my_local_stack.storage)
``` ## flask 的 请求上下文 + ## werkzeug + flask 的 wsgi ```python
from werkzeug.wrappers import Request,Response
from werkzeug.serving import run_simple @Request.application
def app(environ):
print(environ)
return Response("200OK") run_simple("0.0.0.0",9527,app) ``` ```python
from flask import Flask
app = Flask(__name__) if __name__ == '__main__':
app.run(host='0.0.0.0',port=7088)
app.__call__
app.wsgi_app ``` ### 上文 1. 从 **`run()`** 开始 + 进入源码 其中的 >> `self = app = Flask()` + 导入了 `from werkzeug.serving import run_simple` 执行了 `run_simple(host, port, self, **options)` 方法 + 看这一句 `run_simple("0.0.0.0",9527, app) ` 也就是说 self 会执行 >> `self = app = Flask() = self()` + 也就 == app() 对象() 执行 类的 `__call__()` 方法 2. `__call__() ` 执行了 `self.wsgi_app(environ, start_redponse)` **environ = request 原始信息** 3. 进入 `app.wsgi_app` + `def wsgi_app(self, environ, start_response):` >> self = app = Flask() + self 执行了这个方法 `ctx = self.request_context(environ)` 方法中执行了 一个类 `RequestContext(self, environ)` ctx.py文件中 >> 这个对象中 包含了 两个 值 requset/session ctx = requsetcontext --> requset/session + 在这个类中 的 初始化方法 中 `def __init__(self, app, environ, request=None):` >> self = RequestContext ->request/session app = Flask request=None 执行方法 `request = app.request_class(environ)` 这个方法将 environ 序列化 出了 request对象 就是可以使用 request.method requset.form ... 方法 了 + 执行ctx.push() 方法 4. ctx.push() 方法 + 方法中执行了 `top = _request_ctx_stack.top` --> `_request_ctx_stack = LocalStack()` 实例化时的 `__init__` 方法中 初始化了 两个 对象 >> `self._local = {"__storage__": {}, "__ident_func__": get_ident}` >`get_ident` 获取线程 或携程的 id 并没有执行 + `_request_ctx_stack = {"__storage__": {}, "__ident_func__": get_ident}` + `.top ` 方法 执行了 `self._local.stact[-1]` .stack 调用 Local(object): 对象的 `__getattr__(self) 方法` + 方法中 调用 `self.__storage__[self.__ident_func__()][name]` 也就是从 `{"__storage__": {}, "__ident_func__": get_ident}` 中的 `__storage__` 中通过key 取值 此时 `__storage__` 中为 空字典 报错 抛出异常 `AttributeError(name)` 这个异常在上面的 top 方法中 被捕获 返回 一个 None 返回 **top = None** 之后的 if 判断不成立 + 之后又执行了 `_request_ctx_stack.push(self)` 代码 其中的 ` _request_ctx_stack = {"__storage__": {}, "__ident_func__": get_ident}` + 执行了.push(self) 方法 这个self = ctx = requset/session 进入 push() 方法 + `def push(self, obj)`这里面的 self 是 ` _request_ctx_stack = {"__storage__": {}, "__ident_func__": get_ident}` obj = ctx = requset/session 执行 : ` rv = getattr(self._local, 'stack', None)` `self._local = {"__storage__": {}, "__ident_func__": get_ident}` rv = None -> if判断 执行` self._local.stack = rv = [] rv.append(obj) ` 走对象的 `__setattr__(self,name,value)`方法 返回 =>> `{"__storage__": {7088:{"stack": rv=[] }}, "__ident_func__": get_ident}` + 返回 rv.append(obj) ==>`{"__storage__": {7088:{"stack": [ctx=>request/session] }}, "__ident_func__": get_ident}` `rv=[ctx=>request/session] ` 并没有接收返回值 执行完之后 `self._local = {"__storage__": {7088:{"stack": [ctx=>request/session] }}, "__ident_func__": get_ident}` ### 下文 + 使用时才执行 ```python
@app.route("/")
def login():
if requset.method =='POST':
...
# requset.method 之后启动下文
``` + 进入 request ```python
# partial 偏函数
request = LocalProxy(
partial(
_lookup_req_object,
'request'
)
)
``` + `_lookup_req_object` ```python
def _lookup_req_object(name):
top = _request_ctx_stack.top
# _request_ctx_stack = self._local = {"__storage__": {7088:{"stack": [ctx=>request/session] }}, "__ident_func__": get_ident} 调用.top 方法
if top is None:
raise RuntimeError(_request_ctx_err_msg)
return getattr(top, name)
``` + .top 中 执行了 `self._local.stack[-1]` ```python
# 执行 self._local的 __getattr__ 方法
def __getattr__(self, name):
try:
return self.__storage__[self.__ident_func__()][name]
except KeyError:
raise AttributeError(name) # 返回了 [ctx => request / session ]
``` 执行 [-1] 得到 `ctx => request / session` + `top = ctx => request / session` + `getattr(top, name)` name = request 从偏函数传递过来的 方法 得到了 一个 request 对象 及 request 的真身 + 查看` LocalProxy(偏函数没被执行的 )` 实例化 执行`def __init__(self, local, name=None):` local = request 的偏函数 ```python
def __init__(self, local, name=None):
object.__setattr__(self, '_LocalProxy__local', local)
object.__setattr__(self, '__name__', name)
if callable(local) and not hasattr(local, '__release_local__'):
# "local" is a callable that is not an instance of Local or
# LocalManager: mark it as a wrapped function.
object.__setattr__(self, '__wrapped__', local)
``` + request.method 执行 .方法 调用 `__getattr__()` 执行 `getattr(self._get_current_object(), name)` ```python
def _get_current_object(self):
if not hasattr(self.__local, '__release_local__'):
return self.__local()
try:
return getattr(self.__local, self.__name__)
except AttributeError:
raise RuntimeError('no object bound to %s' % self.__name__)
``` `__local = local = requset偏函数执行 = requset` + `getattr(request, name)` name = method + ### 查看 LocalProxy 类 包含了 所有面向对象的 特殊方法 ### 应用上下文 + 会在 离线脚本 的时候使用 + 从 `app.wsgi_app` 中 找到 `ctx.push()` 的方法 + `app_ctx = _app_ctx_stack.top` 这个 就是应用上下文 ```python
app_ctx = _app_ctx_stack.top
if app_ctx is None or app_ctx.app != self.app:
app_ctx = self.app.app_context()
app_ctx.push()
self._implicit_app_ctx_stack.append(app_ctx)
else:
self._implicit_app_ctx_stack.append(None)
``` 1. `_app_ctx_stack = {"__storage__": {}, "__ident_func__": get_ident}`
2. `.top` 方法 返回的还是 None
3. `app_ctx = None` + `app_ctx = self.app.app_context()` 序列化 app 得到 AppContext(app,g) -> requestcontext(request/session) + `app_ctx.push()` 得到 `self._local = {"__storage__": {7088:{"stack": [app_ctx(app, g)] }}, "__ident_func__": get_ident}` g会在 离线脚本 的时候使用 `current_app = LocalProxy(_find_app)` = app = Flask() ```python
def _find_app():
top = _app_ctx_stack.top # 拿到 app_ctx(app,g)
if top is None:
raise RuntimeError(_app_ctx_err_msg)
return top.app # 返回一个 app 独立的 app
``` 请求结束之后执行 pop 方法 删除缓存 `g = LocalProxy(partial(_lookup_app_object, 'g'))`

flask 请求上下文的更多相关文章

  1. Flask(4)- flask请求上下文源码解读、http聊天室单聊/群聊(基于gevent-websocket)

    一.flask请求上下文源码解读 通过上篇源码分析,我们知道了有请求发来的时候就执行了app(Flask的实例化对象)的__call__方法,而__call__方法返回了app的wsgi_app(en ...

  2. flask 请求上下文源码(转)

    本篇阅读目录 一.flask请求上下文源码解读 二.http聊天室(单聊/群聊)- 基于gevent-websocket 回到顶部 转:https://www.cnblogs.com/li-li/p/ ...

  3. flask请求上下文

    先看一个例子: #!/usr/bin/env python # -*- coding:utf-8 -*- import threading # local_values = threading.loc ...

  4. Flask请求上下文源码讲解,简单的群聊单聊web

    请求上下文流程图 群聊html代码 <!DOCTYPE html> <html lang="en"> <head> <meta chars ...

  5. flask请求上下文 (转)

    本篇阅读目录 一.flask中的CBV 二.werkzeug + 上下文初步解读 三.偏函数和线程安全 回到顶部 一.flask中的CBV 对比django中的CBV,我们来看一下flask中的CBV ...

  6. flask请求上下文源码分析

    一.什么是上下文 每一段程序都有很多外部变量,只有像add这种简单的函数才是没有外部变量的,一旦你的一段程序有了外部变量,这段程序就不完整了,不能独立运行,你为了使他们能运行,就要给所有的外部变量一个 ...

  7. Flask请求上下文request

  8. python 全栈开发,Day139(websocket原理,flask之请求上下文)

    昨日内容回顾 flask和django对比 flask和django本质是一样的,都是web框架. 但是django自带了一些组件,flask虽然自带的组件比较少,但是它有很多的第三方插件. 那么在什 ...

  9. flask的请求上下文源码解读

    一.flask请求上下文源码解读 通过上篇源码分析( ---Flask中的CBV和上下文管理--- ),我们知道了有请求发来的时候就执行了app(Flask的实例化对象)的__call__方法,而__ ...

随机推荐

  1. (4.28)for xml path 在合并拆分上的作用演示

    for xml path 用于合并与拆分 1.合并 很多时候需要在SQL Server中创建逗号分隔列表.这可以使用SQL Server的DOR XML PATH功能完成.与select语句一起使用时 ...

  2. rm:删除目录和文件

    [root@linux-node- sss]# rm soft.txt //删除文件 rm: remove regular empty file ‘soft.txt’? y [root@linux-n ...

  3. 20170719 Mysql 配置远端Mysql访问,增加表/存储过程

    -- 1 .在windows 环境中安装Mysql 会按照到默认的C盘当中,如何修改呢--? -- 2. 如何只安装客户端不安装Mysql 数据库服务  --? -- 3. 表的特殊列,默认采用函数值 ...

  4. SQL SERVER 2016研究一

    一.安装SQL Server 2016 安装检查提示异常: 安装补丁: 根据系统判断补丁安装顺序,按顺序安装 Windows server 2012 R2 安装SQL补丁如下以及JDK: 安装完成 二 ...

  5. sap 对dynamic query parameters 设置条件。

  6. 创建数据表,自定义data element, field等。

    参考:https://wenku.baidu.com/view/253ddbfaa5e9856a561260da.html 一:创建域. 使用T-CODE 11 搜索 数据操作系统. 选择domain ...

  7. [py]软件编程知识骨架+py常见数据结构

    认识算法的重要性 - 遇到问题? 学完语言,接到需求,没思路? 1.学会了语言,能读懂别人的代码, 但是自己没解决问题的能力,不能够把实际问题转换为代码,自己写出来.(这是只是学会一门语言的后果),不 ...

  8. matplot绘图

    import matplotlib.pyplot as pltimport numpy as npimport matplotlib # 设置matplotlib正常显示中文和负号matplotlib ...

  9. mac-Navicat Premium 12连接Oracle

    安装及破解: https://pan.baidu.com/s/1ip0oM9p856k34t3cIExa7Q(提取码请留言) Navicat连接了Oracle数据库 测试连接失败,提示:hostnam ...

  10. vue的分页组件

    <template> <div class="page-nav"> <div class="page-btn-wrap"> ...