Flask 上下文机制和线程隔离
1. 计算机科学领域的任何问题都可以通过增加一个间接的中间层来解决, 上下文机制就是这句话的体现。
2. 如果一次封装解决不了问题,那就再来一次
上下文:相当于一个容器,保存了Flask程序运行过程中的一些信息 源码:flask/ctx.py
请求上下文:Flask从客户端收到请求时,要让视图函数能访问一些对象,这样才能处理请求,要想让视图函数能够访问请求对象,一个显而易见的方式是将其作为参数传入视图函数,不过这会导致程序中的每个视图函数都增加一个参数,除了访问请求对象,如果视图函数在处理请求时还要访问其他对象,情况会变得更糟。为了避免大量可有可无的参数把视图函数弄得一团糟,Flask使用上下文临时把某些对象变为全局可访问。这就是一种重构设计思路。
- request 封装了HTTP请求的内容,针对http请求,也是一种符合WSGI接口规范的设计(关于WSGI可参考我对该协议的理解和实现demo mini-wsgi-web),如
request.args.get('user')
- session 用来记录请求会话中的信息,针对的是用户信息,如
session['name'] = user.id
- request 封装了HTTP请求的内容,针对http请求,也是一种符合WSGI接口规范的设计(关于WSGI可参考我对该协议的理解和实现demo mini-wsgi-web),如
应用上下文:应用程序上下文,用于存储应用程序中的变量
- current_app 存储应用配置,数据库连接等应用相关信息
- g变量 作为flask程序全局的一个临时变量, 充当者中间媒介的作用,我们可以通过它传递一些数据,g保存的是当前请求的全局变量,不同的请求会有不同的全局变量,通过不同的thread id区别
# context locals
# 使用代理模式 LocalProxy
_request_ctx_stack = LocalStack()
_app_ctx_stack = LocalStack()
current_app = LocalProxy(_find_app)
request = LocalProxy(partial(_lookup_req_object, 'request'))
session = LocalProxy(partial(_lookup_req_object, 'session'))
g = LocalProxy(partial(_lookup_app_object, 'g'))
1. working outside application context
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# __author__ = '__JonPan__'
from flask import Flask, current_app
app = Flask(__name__)
a = current_app
# is_debug = current_app.config['DEBUG']
@app.route('/')
def index():
return '<h1>Hello World. Have a nice day! </1>'
if __name__ == '__main__':
app.run(host='localhost', port=8888)
报错:
Exception has occurred: RuntimeError
Working outside of application context.
2. flask 上下文出入栈
flask上下文对象出入栈模型图
在应用开发中可用直接引用current_app
不会报错,是因为当在一个请求中使用的时候,flask会判断_app_ctx_stack
栈顶是否有可用对象,如果没有就会自动推入一个App. 我们获取的current_app
就是获取的栈顶元素
# flask/globals.py
def _find_app():
top = _app_ctx_stack.top
if top is None:
raise RuntimeError(_app_ctx_err_msg)
return top.app
current_app = LocalProxy(_find_app)
修改代码:将app对象推入栈顶
# ...
# 将app_context 推入栈中
ctx = app.app_context()
ctx.push()
a = current_app
is_debug = current_app.config['DEBUG']
ctx.pop()
# ...
# 更有pythonnic的写法
# 将app_context 推入栈中
# with app.app_context():
# a = current_app
# is_debug = current_app.config['DEBUG']
不在出现unbound
状态。可正常运行
既然flask会自动帮我们检测栈顶元素是否存在,为什么我们还要做这一步操作,当我们在写离线应用,或者单元测试的时候就需要用到,因为请求是模拟的,不是在application context中的了。
3. python中的上文管理器
实现了__enter__
和__exit__
方法的对象就是一个上文管理器。
class MyResource:
def __enter__(self):
print('connect ro resource')
return self
def __exit__(self, exc_type, exc_value, tb):
if tb:
print('process exception')
else:
print('no exception')
print('close resource connection')
# return True
# return False
def query(self):
print('query data')
try:
with MyResource() as r:
1/0
r.query()
except Exception as e:
print(e)
with MyResour ce() as r
as 的别名r指向的不是上想问管理器对象,而是__enter__
方法返回的值,在以上代码确实是返回了对象本身。
__exit__
方法 处理退出上下文管理器对象时的一些资源清理工作,并处理异常,三个参数
- exc_type 异常类型
- exc_value 异常原因解释
- tb traceback
__exit__
其实是有返回值的,return True
表示,异常信息已经在本方法中处理,外部可不接收异常,return False
表示将异常抛出给上层逻辑处理,默认不写返回,即默认值是None
, None
也表示False
线程隔离机制
如何实现一个Reqeust 指向多个请求实例,且要区分该实例对象所绑定的用户?
字典:
request = {'key1': val1, 'key2': val2}
flask引用 werkzeug
中的 local.Local
实现线程隔离
# werkzeug\local.py
class Local(object):
__slots__ = ('__storage__', '__ident_func__')
def __init__(self):
object.__setattr__(self, '__storage__', {})
object.__setattr__(self, '__ident_func__', get_ident)
def __iter__(self):
return iter(self.__storage__.items())
def __call__(self, proxy):
"""Create a proxy for a name."""
return LocalProxy(self, proxy)
def __release_local__(self):
self.__storage__.pop(self.__ident_func__(), None)
def __getattr__(self, name):
try:
return self.__storage__[self.__ident_func__()][name]
except KeyError:
raise AttributeError(name)
def __setattr__(self, name, value):
# 获取当前线程的id
ident = self.__ident_func__()
storage = self.__storage__
try:
storage[ident][name] = value
except KeyError:
storage[ident] = {name: value}
def __delattr__(self, name):
try:
del self.__storage__[self.__ident_func__()][name]
except KeyError:
raise AttributeError(name)
可以看到是使用线程ID来绑定不同的上线文对象。
LocalStack
class LocalStack(object):
"""This class works similar to a :class:`Local` but keeps a stack
of objects instead. This is best explained with an example::
>>> ls = LocalStack()
>>> ls.push(42)
>>> ls.top
42
>>> ls.push(23)
>>> ls.top
23
>>> ls.pop()
23
>>> ls.top
42
They can be force released by using a :class:`LocalManager` or with
the :func:`release_local` function but the correct way is to pop the
item from the stack after using. When the stack is empty it will
no longer be bound to the current context (and as such released).
By calling the stack without arguments it returns a proxy that resolves to
the topmost item on the stack.
.. versionadded:: 0.6.1
"""
def __init__(self):
self._local = Local()
def __release_local__(self):
self._local.__release_local__()
def _get__ident_func__(self):
return self._local.__ident_func__
def _set__ident_func__(self, value):
object.__setattr__(self._local, '__ident_func__', value)
__ident_func__ = property(_get__ident_func__, _set__ident_func__)
del _get__ident_func__, _set__ident_func__
def __call__(self):
def _lookup():
rv = self.top
if rv is None:
raise RuntimeError('object unbound')
return rv
return LocalProxy(_lookup)
def push(self, obj):
"""Pushes a new item to the stack"""
rv = getattr(self._local, 'stack', None)
if rv is None:
self._local.stack = rv = []
rv.append(obj)
return rv
def pop(self):
"""Removes the topmost item from the stack, will return the
old value or `None` if the stack was already empty.
"""
stack = getattr(self._local, 'stack', None)
if stack is None:
return None
elif len(stack) == 1:
release_local(self._local)
return stack[-1]
else:
return stack.pop()
@property
def top(self):
"""The topmost item on the stack. If the stack is empty,
`None` is returned.
"""
try:
return self._local.stack[-1]
except (AttributeError, IndexError):
return None
Local使用字典的方式实现线程隔离,LocalStack 则封装Local实现了线程隔离的栈结构
总结
- flask上下文的实现使用了设计模式中的代理模式
- 线程隔离的实现机制就是利用线程ID+字典, 使用线程隔离的意义在于:使当前线程能够正确引用到他自己所创建的对象,而不是引用到其他线程所创建的对象
- 请求上下文, 应用上线文是线程隔离的对象,current_app并没有使用线程隔离,全局唯一
current_app -> (LocalStack.top = AppContext top.app = Flask)
request -> (LocalStack.top = RequestContext.top.request = Request)
参考资料
Flask 上下文机制和线程隔离的更多相关文章
- 六十七:flask上下文之Local线程隔离对象
Local对象在flask中,类似于request对象,其实是绑定到了werkzeug.local.Local对象上,这样即使是同一个对象,在多线程中都是隔离的,类似的对象还有session以及g对象 ...
- flask高级编程 LocalStack 线程隔离
转:https://www.cnblogs.com/wangmingtao/p/9372611.html 30.LocalStack作为线程隔离对象的意义 30.1 数据结构 限制了某些能力 30 ...
- 4.1 python类的特殊成员,偏函数,线程安全,栈,flask上下文
目录 一. Python 类的特殊成员(部分) 二. Python偏函数 1. 描述 2. 实例一: 取余函数 3. 实例二: 求三个数的和 三. 线程安全 1. 实例一: 无线程,消耗时间过长 2. ...
- 详解Flask上下文
上下文是在Flask开发中的一个核心概念,本文将通过阅读源码分享下其原理和实现. Flask系列文章: Flask开发初探 WSGI到底是什么 Flask源码分析一:服务启动 Flask路由内部实现原 ...
- 六十九:flask上下文之线程隔离的g对象的使用
保存全局对象的g对象g对象是在整个flask应用运行期间都是可以使用的,并且也是和request一样,是线程隔离的,这个对象是专门用来存放开发者自己定义的一些数据,方便在整个flask程序中都可以使用 ...
- Flask核心机制--上下文源码剖析
一.前言 了解过flask的python开发者想必都知道flask中核心机制莫过于上下文管理,当然学习flask如果不了解其中的处理流程,可能在很多问题上不能得到解决,当然我在写本篇文章之前也看到了很 ...
- Flask上下文管理机制
前引 在了解flask上下文管理机制之前,先来一波必知必会的知识点. 面向对象双下方法 首先,先来聊一聊面向对象中的一些特殊的双下划线方法,比如__call__.__getattr__系列.__get ...
- flask LOCAL线程隔离技术
from threading import Thread from werkzeug.local import Local local = Local()#实例化一个线程隔离对象 request = ...
- 使用Hystrix的插件机制,解决在使用线程隔离时,threadlocal的传递问题
背景 在我们的项目中,比较广泛地使用了ThreadLocal,比如,在filter层,根据token,取到用户信息后,就会放到一个ThreadLocal变量中:在后续的业务处理中,就会直接从当前线程, ...
随机推荐
- [julia][学习笔记]julia的安装
目录 julia的安装 下载地址 下载后安装 IDE Julia Pro 安装方法 使用方法(以hello world为例) vscode的Julia插件 下载vscode 安装vscode Juli ...
- epic无法登陆怎么办【百分百解决】
epic无法登陆怎么办(感谢吾爱破解吧www.52pjb.net)站长提供的文章教程以及解决思路,谢谢大佬,更详细的解决办法可以参考下他的网站解决的方法实例! epic登陆不上去怎么办?随着GTA5免 ...
- kali系统安装google拼音
1.设置多线程下载 /bin/bash -c "$(curl -sL https://git.io/vokNn)" 2.打开终端,输入下面的命令 apt-fast install ...
- Unit2-窝窝牌电梯
全文共2329字,推荐阅读时间10~15分钟. 文章共分四个部分: 作业分析 评测相关 重构策略 课程体验感受 作业分析 Unit2要求我们模拟现实生活中的电梯调度情景,迭代路径是单电梯->多电 ...
- vc++,MfC ,cstring与char相互转换知识
//mapName = mapString;//----------------------原始- string mapName; CString strtemp,strtemp2; //char t ...
- 01 . MongoDB简介及部署配置
简介 什么是MongoDB? MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统. 在高负载的情况下,添加更多的节点,可以保证服务器性能. MongoDB 旨在为WEB应用 ...
- 获取系统的IP地址
获取linux主机的IP地址 问题描述 在很多软件配置过程中,都需要设置ID信息,通常我选择使用系统配置的eth0网卡的IP地址信息,比如salt-minion-id,在通过cobbler批量安装操作 ...
- Java多线程之volatile详解
本文目录 从多线程交替打印A和B开始 Java 内存模型中的可见性.原子性和有序性 Volatile原理 volatile的特性 volatile happens-before规则 volatile ...
- 二叉查找树、平衡二叉树(AVLTree)、平衡多路查找树(B-Tree),B+树
B+树索引是B+树在数据库中的一种实现,是最常见也是数据库中使用最为频繁的一种索引. B+树中的B代表平衡(balance),而不是二叉(binary),因为B+树是从最早的平衡二叉树演化而来的. 在 ...
- HttpClient优化
HttpClient优化思路: 1.池化 2.长连接 3.httpclient和httpget复用 4.合理的配置参数(最大并发请求数,各种超时时间,重试次数) 5.异步 6.多读源码 1.背景我们有 ...