线程锁,threadinglocal,线程池,生产者消费者模型

1.线程锁

  1.锁Lock(只能锁一次)

  

 import threading
import time v = []
lock = threading.Lock() def func(arg):
lock.acquire()
v.append(arg)
time.sleep(1)
m = v[-1]
print(arg,m)
lock.release() for i in range(10):
t = threading.Thread(target=func,args=(i,))
t.start()

  2.锁RLock(可以锁多次)

 import threading
import time v = []
lock = threading.RLock() def func(arg):
lock.acquire()
lock.acquire() v.append(arg)
time.sleep(1)
m = v[-1]
print(arg,m) lock.release()
lock.release() for i in range(10):
t = threading.Thread(target=func,args=(i,))
t.start()

  3.锁semaphore(一次放n个)

 import threading
import time lock = threading.BoundedSemaphore(3)#一次放三个 def func(arg):
lock.acquire() print(arg)
time.sleep(2) lock.release() for i in range(20):
t = threading.Thread(target=func,args=(i,))
t.start()

  4.锁Condition(一次放指定个数)

 import time
import threading lock = threading.Condition() #方式一 def func(arg):
print("线程进来了")
lock.acquire()
lock.wait()#加锁 print(arg)
time.sleep(1) lock.release() for i in range(10):
t = threading.Thread(target=func,args=(i,))
t.start() while True:
num = int(input(">>>"))
lock.acquire()
lock.notify(num)
lock.release() #方式二 def func():
print("来执行函数了")
input(">>>")
ct = threading.current_thread()#获取当前线程
ct.getName()
return True def func1(arg):
print("线程进来了")
lock.wait_for(func)
print(arg)
time.sleep(1) for i in range(10):
t = threading.Thread(target=func1,args=(i,))
t.start()

  5.锁Event(一次放所有)

 import time
import threading lock = threading.Event() def func(arg):
print("线程来了")
lock.wait()#加锁
print(arg) for i in range(10):
t = threading.Thread(target=func,args=(i,))
t.start() input(">>>")
lock.set()#释放锁 lock.clear()#再次加锁 for i in range(10):
t = threading(target=func,args=(i,))
t.start() input(">>>")
lock.set()

2.threadinglocal

 import time
import threading v = threading.local() def func(arg):
#内部会为当前线程创建一个空间用于存储
v.local = arg
time.sleep(2)
print(v.local,arg)#去当前线程自己的空间取值 for i in range(10):
t = threading.Thread(target=func,args=(i,))
t.start()

threadinglocal原理

 import time
import threading data_dict = {} def func(arg):
ident = threading.get_ident()
data_list[ident] = arg
time.sleep(1)
print(data_dict[ident],arg) for i in range(10):
t = threading.Thread(target=func,args=(i,))
t.start()
 import time
import threading info = {} class Local(object): def __getattr__(self,item):
ident = threading.get_ident()
return info[ident][item] def __setattr__(self,key,value):
ident = threading.get_ident()
if ident in info:
info[ident][key] = value
else:
info[ident] = {key:value} obj = Local() def func(arg):
obj.local = arg#调用对象的__setattr__方法
time.sleep(1)
print(obj.local,arg) for i in range(10):
t = threading.Thread(target=func,args=(i,))
t.start()

3.线程池

 from concurrent.futures import ThreadPoolExecutor
import time def task(a1,a2):
time.sleep(2)
print(a1,a2) #创建一个线程池(最多五个线程)
pool = ThreadPoolExecutor(5) for i in range(100):
#去线程池申请一个线程,让线程执行task函数
pool.submit(task,i,10)

4.生产者消费者模型

 import time
import queue
import threading q = queue.Queue()#线程安全 def producer(id):
while True:
time.sleep(1)
q.put("")
print("生产了一个%s"% id) for i in range(1,4):
t = threading.Thread(target=func,args=(i,))
t.start() def consumer(id):
while True:
time.sleep(1)
v1 = q.get()
print("消费了一个%s"% id) for i in range(1,3):
t = threading.Thread(target=consumer,args=(i,))
t.start()
上一篇:用极简方式实现新浪新版本特性展示效果--view的图片轮播


下一篇:mysql中kill掉所有锁表的进程