5.3 Flask中的线程隔离
1. Local对象
class Local(object):
__slots__ = ('__storage__', '__ident_func__')
def __init__(self):
# 一个私有变量__storage__字典
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:
# 把线程id号作为key保存了起来
storage[ident] = {name: value}
def __delattr__(self, name):
try:
del self.__storage__[self.__ident_func__()][name]
except KeyError:
raise AttributeError(name)使用线程隔离和不适用线程隔离的区别
2. 线程隔离的栈:LocalStack

Local,Local Stack,字典的关系
3.LocalStack的基本用法
4.LocalStack作为线程隔离对象的意义
5.Flask中被线程隔离的对象
Last updated
Was this helpful?

