补充:|补充: `threading.local`,session源码

threading.local 【补充:|补充: `threading.local`,session源码】多个线程修改同一个数据,复制多份变量给每个线程用,为每个线程开辟一块空间进行数据存储
不用threading.local

# 不用local from threading import Thread import time lqz = -1 def task(arg): global lqz lqz = arg # time.sleep(2) print(lqz)for i in range(10): t = Thread(target=task,args=(i,)) t.start()

threading.local使用
from threading import Thread from threading import local import time from threading import get_ident # 特殊的对象 lqz = local() def task(arg): # 对象.val = 1/2/3/4/5 lqz.value = https://www.it610.com/article/arg time.sleep(2) print(lqz.value) for i in range(10): t = Thread(target=task,args=(i,)) t.start()

通过字典自定义threading.local(函数)
from threading import get_ident,Thread import time storage = {} def set(k,v): ident = get_ident() if ident in storage: storage[ident][k] = v else: storage[ident] = {k:v} def get(k): ident = get_ident() return storage[ident][k] def task(arg): set('val',arg) v = get('val') print(v)for i in range(10): t = Thread(target=task,args=(i,)) t.start()

面向对象版
from threading import get_ident,Thread import time class Local(object): storage = {} def set(self, k, v): ident = get_ident() if ident in Local.storage: Local.storage[ident][k] = v else: Local.storage[ident] = {k: v} def get(self, k): ident = get_ident() return Local.storage[ident][k] obj = Local() def task(arg): obj.set('val',arg) v = obj.get('val') print(v) for i in range(10): t = Thread(target=task,args=(i,)) t.start()

通过setattr和getattr实现
from threading import get_ident,Thread import time class Local(object): storage = {} def __setattr__(self, k, v): ident = get_ident() if ident in Local.storage: Local.storage[ident][k] = v else: Local.storage[ident] = {k: v} def __getattr__(self, k): ident = get_ident() return Local.storage[ident][k] obj = Local() def task(arg): obj.val = arg print(obj.val) for i in range(10): t = Thread(target=task,args=(i,)) t.start()

每个对象有自己的存储空间(字典)
from threading import get_ident,Thread import time class Local(object): def __init__(self): object.__setattr__(self,'storage',{}) def __setattr__(self, k, v): ident = get_ident() if ident in self.storage: self.storage[ident][k] = v else: self.storage[ident] = {k: v} def __getattr__(self, k): ident = get_ident() return self.storage[ident][k] obj = Local() def task(arg): obj.val = arg obj.xxx = arg print(obj.val) for i in range(10): t = Thread(target=task,args=(i,)) t.start()

兼容线程和协程
try: from greenlet import getcurrent as get_ident except Exception as e: from threading import get_ident from threading import Thread import time class Local(object): def __init__(self): object.__setattr__(self,'storage',{}) def __setattr__(self, k, v): ident = get_ident() if ident in self.storage: self.storage[ident][k] = v else: self.storage[ident] = {k: v} def __getattr__(self, k): ident = get_ident() return self.storage[ident][k] obj = Local() def task(arg): obj.val = arg obj.xxx = arg print(obj.val) for i in range(10): t = Thread(target=task,args=(i,)) t.start()

session源码分析
SecureCookieSessionInterface -open_session -save_session

    推荐阅读