flask LOCAL线程隔离技术
2021-05-17 12:28
标签:class 实例 def lob oca alt 隔离 图片 reading flask LOCAL线程隔离技术 标签:class 实例 def lob oca alt 隔离 图片 reading 原文地址:https://www.cnblogs.com/wuheng-123/p/9747034.htmlfrom threading import Thread
from werkzeug.local import Local
local = Local()#实例化一个线程隔离对象
request = ‘123‘
class MyThread(Thread):
def run(self):
global request
request = ‘abc‘
print(‘子线程‘,request)
mythread = MyThread()
mythread.start()
mythread.join()
print(‘主线程‘,request)#线程之前是共享的,要想线程隔离,就用到flask中的Local
#使用Local线程隔离会怎么样
local.request = ‘456‘
class LocalThead(Thread):
def run(self):
local.request = ‘def‘
print(‘子线程‘,local.request)
localthead = LocalThead()
localthead.start()
localthead.join()
print(‘主线程‘,local.request)#线程完全隔离了
from threading import local #python 的多线程也有local线程隔离,只要绑定到local的属性,都是线程隔离的
上一篇:汇编语言的基础知识
下一篇:python基本数据类型