线程隔离

 

import threading
import time

from werkzeug.local import Local, LocalStack


# Local是实现线程隔离的方法对象, LocalStack是封装了实现线程隔离方法的栈结构对象

class A:
    b = 1


my_obj = Local()
my_obj.b = 1


def worker():
    # 新线程
    my_obj.b = 2
    print(my_obj.b)


new_t = threading.Thread(target=worker, name='wang_thread')
new_t.start()
time.sleep(1)
# 主线程
print(my_obj.b)
my_stack = LocalStack()
my_stack.push(1)


def worker_1():
    print(my_stack.top)
    my_stack.push(2)
    print(my_stack.top)


new_w = threading.Thread(target=worker_1, name='wang_1')
new_w.start()
time.sleep(1)
print(my_stack.top)
# 主线程和子线程中的值是互不影响的

  

上一篇:同一服务器创建多个MySQL服务过程记录


下一篇:数据库系列之MySQL表ibd文件删除恢复