""" @Author : Johnny Leaf @Time : 2019/5/20 15:50 @Software : PyCharm @File : test3.py """ import threading import time from werkzeug.local import Local class A: b = 1 my_job = Local() my_job.b = 1 def worker(): my_job.b = 2 print("in new thread b is " + str(my_job.b)) new_t = threading.Thread(target=worker, name='johnny leaf') new_t.start() time.sleep(1) print("the mainthread b is " + str(my_job.b))
#!/usr/bin/env python # -*- coding:utf-8 -*- import threading import time from werkzeug.local import Local # class A: # b = 1 # my_obj = A() my_obj = Local() my_obj.b = 1 def worker(): my_obj.b = 2 print('in new thread b is: {}'.format(my_obj.b)) new_t = threading.Thread(target=worker, name='qiyue_thread') new_t.start() time.sleep(1) # 主线程 print('in main thread b is: {}'.format(my_obj.b))
# -*- coding: utf-8 -*- # @Time : 2018/7/24 0024 16:46 # @Author : YJob import threading import time from werkzeug.local import Local # 普通类 没有做到线程隔离 class A: b = 1 new_a = A() new_local = Local() new_local.b = 1 def work(): new_a.b = 2 print('i am thread new_a.b:' + str(new_a.b)) def work1(): new_local.b = 2 print('i am thread new_local.b:' + str(new_local.b)) new_t1 = threading.Thread(target=work) new_t1.start() new_t2 = threading.Thread(target=work1) new_t2.start()
# IO密集型的程序 查询数据库,请求网络资源,读写文件 # flask web 框架 # 请求,线程 # flask 开启多少个线程来处理请求 # nginx apache tomact iis # 线程隔离 werkzeug local Local dict # localstack local 使用字典的实现的线程隔离 # 线程隔离的栈结构 # {thread_id1:Lvalue1,thread_id2:value2...} # 封装 from werkzeug.local import Local import threading import time my_obj=Local() my_obj.b=1 def worker(): my_obj.b=2 print('in new thread b is:'+str(my_obj.b)) #new thread new_t=threading.Thread(target=worker, name='qiyue_thread') new_t.start() time.sleep(1) # main thread print('in main thread b is:'+str(my_obj.b))
# -*- coding: utf-8 -*- import threading import time from werkzeug.local import Local my_1 = Local() my_1.b = 0 # 新线程中,b的值被修改 def worker(): my_1.b = 1 print(my_1.b) new_t = threading.Thread(target=worker) new_t.start() time.sleep(1) # 主线程中,b的值没有改变 print(my_1.b) tup = ('09EC9EBEC4890112E0530AF680F21CD4', '09EC9EBEC4890112E0530AF680F21CD4', '010D7EE10E890330E0530AF680F1964D', '058080BB682E021AE0530AF680F16E05', '06791828B3EB00C8E0530AF680F16DBF', '058A7C9902480244E0530AF680F106A8', '062293B400FA0268E0530AF680F10810', '05FAF21EA2470326E0530AF680F1169C', '07B3F4B66764032AE0530AF680F2DFB1', '09AF3ADB502E0360E0530AF680F25B2F', '08A76B8A692702BCE0530AF680F2E56A', '09D9833747F7023CE0530AF680F20ABB', '05D09805F19F0120E0530AF680F15582') print(len(tup))
import threading import time from werkzeug.local import Local class A: b = 1 # obj = A() obj = Local() obj.b = 1 def worker(): obj.b = 2 new_t = threading.Thread(target=worker) new_t.start() time.sleep(1) print(obj.b)
from flask import Flask, current_app import threading import time from werkzeug.local import Local t = threading.current_thread() print(t.getName()) class A: t = 1 l = Local() l.b = 1 def worker(): t = threading.current_thread() # print(t.getName()) l.b = 2 time.sleep(7) print('hello') t1 = threading.Thread(target=worker, daemon=True) t1.start() print(l.b) # t1.join()