from 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的属性,都是线程隔离的
原文地址:https://www.cnblogs.com/wuheng-123/p/9747034.html
时间: 2024-10-08 17:28:08