同步锁/互斥锁 (Lock)
import time import threading def sub(): global num #在每个线程中都获取这个全局变量 #num-=1 temp=num time.sleep(0.1) num =temp-1 # 对此公共变量进行-1操作 num = 100 #设定一个共享变量 thread_list = [] for i in range(100): t = threading.Thread(target=sub) t.start() thread_list.append(t) for t in thread_list: #等待所有线程执行完毕 t.join() print('Result: ', num) 运行结果: 99
我们想要的结果是多线程实现递减至0 ,但是很明显没有实现。
下面这段代码更改了一下睡眠时间,依然结果不会是0 。
import time import threading def sub(): global num #在每个线程中都获取这个全局变量 #num-=1 temp=num time.sleep(0.001) num =temp-1 # 对此公共变量进行-1操作 num = 100 #设定一个共享变量 thread_list = [] for i in range(100): t = threading.Thread(target=sub) t.start() thread_list.append(t) for t in thread_list: #等待所有线程执行完毕 t.join() print('Result: ', num) 运行结果: 大于0 但是小于100的数
锁通常被用来实现对共享资源的同步访问。
import threading R=threading.Lock() #为每一个共享资源创建一个Lock对象 R.acquire() #当你需要访问该资源时,调用acquire方法来获取锁对象(如果其它线程已经获得了该锁,则当前线程需等待其被释放) ''' 对公共数据的操作 ''' R.release() #待资源访问完后,再调用release方法释放锁:
import time import threading def sub(): global num #在每个线程中都获取这个全局变量 lock.acquire() temp=num time.sleep(0.001) num =temp-1 # 对此公共变量进行-1操作 lock.release() num = 100 #设定一个共享变量 thread_list = [] lock=threading.Lock() for i in range(100): t = threading.Thread(target=sub) t.start() thread_list.append(t) for t in thread_list: #等待所有线程执行完毕 t.join() print('Result: ', num) 运行结果: Result: 0
这样就解决了我们的问题。
为什么有了GIL,还需要线程同步? 多线程环境下必须存在资源的竞争,那么如何才能保证同一时刻只有一个线程对共享资源进行存取? 加锁, 对, 加锁可以保证存取操作的唯一性, 从而保证同一时刻只有一个线程对共享数据存取.
死锁与递归锁
import time import threading class Mythread(threading.Thread): def __init__(self): threading.Thread.__init__(self) def run(self): self.foo() def foo(self): locka.acquire() print('I am %s got locka---%s'%(self.name,time.ctime())) lockb.acquire() print('I am %s got lockb---%s'%(self.name,time.ctime())) lockb.release() locka.release() if __name__ == '__main__': locka=threading.Lock() lockb=threading.Lock() for i in range(10): t=Mythread() t.start() 运行效果: I am Thread-1 got locka---Fri Jul 21 14:30:32 2017 I am Thread-1 got lockb---Fri Jul 21 14:30:32 2017 I am Thread-2 got locka---Fri Jul 21 14:30:32 2017 I am Thread-2 got lockb---Fri Jul 21 14:30:32 2017 I am Thread-3 got locka---Fri Jul 21 14:30:32 2017 I am Thread-3 got lockb---Fri Jul 21 14:30:32 2017 I am Thread-4 got locka---Fri Jul 21 14:30:32 2017 I am Thread-4 got lockb---Fri Jul 21 14:30:32 2017 I am Thread-5 got locka---Fri Jul 21 14:30:32 2017 I am Thread-5 got lockb---Fri Jul 21 14:30:32 2017 I am Thread-6 got locka---Fri Jul 21 14:30:32 2017 I am Thread-6 got lockb---Fri Jul 21 14:30:32 2017 I am Thread-7 got locka---Fri Jul 21 14:30:32 2017 I am Thread-7 got lockb---Fri Jul 21 14:30:32 2017 I am Thread-8 got locka---Fri Jul 21 14:30:32 2017 I am Thread-8 got lockb---Fri Jul 21 14:30:32 2017 I am Thread-9 got locka---Fri Jul 21 14:30:32 2017 I am Thread-9 got lockb---Fri Jul 21 14:30:32 2017 I am Thread-10 got locka---Fri Jul 21 14:30:32 2017 I am Thread-10 got lockb---Fri Jul 21 14:30:32 2017
所谓死锁: 是指两个或两个以上的进程或线程在执行过程中,因争夺资源而造成的一种互相等待的现象,若无外力作用,它们都将无法推进下去。此时称系统处于死锁状态或系统产生了死锁,这些永远在互相等待的进程称为死锁进程。
import time import threading class Mythread(threading.Thread): def __init__(self): threading.Thread.__init__(self) def run(self): self.foo() self.bar() def foo(self): locka.acquire() print('I am %s got locka---%s'%(self.name,time.ctime())) lockb.acquire() print('I am %s got lockb---%s'%(self.name,time.ctime())) lockb.release() locka.release() def bar(self): lockb.acquire() print('I am %s got lockb---%s'%(self.name,time.ctime())) locka.acquire() print('I am %s got locka---%s'%(self.name,time.ctime())) locka.release() lockb.release() if __name__ == '__main__': locka=threading.Lock() lockb=threading.Lock() for i in range(10): t=Mythread() t.start()
运行效果图:
解决死锁状态的递归锁出现了。
在Python中为了支持在同一线程中多次请求同一资源,python提供了可重入锁RLock。
这个RLock内部维护着一个Lock和一个counter变量,counter记录了acquire的次数,从而使得资源可以被多次require。直到一个线程所有的acquire都被release,其他的线程才能获得资源。上面的例子如果使用RLock代替Lock,则不会发生死锁:
import time import threading class Mythread(threading.Thread): def __init__(self): threading.Thread.__init__(self) def run(self): self.foo() self.bar() def foo(self): Rlock.acquire() print('I am %s got locka---%s'%(self.name,time.ctime())) Rlock.acquire() print('I am %s got lockb---%s'%(self.name,time.ctime())) Rlock.release() Rlock.release() def bar(self): Rlock.acquire() print('I am %s got lockb---%s'%(self.name,time.ctime())) Rlock.acquire() print('I am %s got locka---%s'%(self.name,time.ctime())) Rlock.release() Rlock.release() if __name__ == '__main__': Rlock=threading.RLock() for i in range(10): t=Mythread() t.start() 运行效果: I am Thread-1 got locka---Fri Jul 21 14:42:08 2017 I am Thread-1 got lockb---Fri Jul 21 14:42:08 2017 I am Thread-2 got locka---Fri Jul 21 14:42:08 2017 I am Thread-2 got lockb---Fri Jul 21 14:42:08 2017 I am Thread-2 got lockb---Fri Jul 21 14:42:08 2017 I am Thread-2 got locka---Fri Jul 21 14:42:08 2017 I am Thread-1 got lockb---Fri Jul 21 14:42:08 2017 I am Thread-1 got locka---Fri Jul 21 14:42:08 2017 I am Thread-3 got locka---Fri Jul 21 14:42:08 2017 I am Thread-3 got lockb---Fri Jul 21 14:42:08 2017 I am Thread-4 got locka---Fri Jul 21 14:42:08 2017 I am Thread-4 got lockb---Fri Jul 21 14:42:08 2017 I am Thread-5 got locka---Fri Jul 21 14:42:08 2017 I am Thread-5 got lockb---Fri Jul 21 14:42:08 2017 I am Thread-3 got lockb---Fri Jul 21 14:42:08 2017 I am Thread-3 got locka---Fri Jul 21 14:42:08 2017 I am Thread-6 got locka---Fri Jul 21 14:42:08 2017 I am Thread-6 got lockb---Fri Jul 21 14:42:08 2017 I am Thread-6 got lockb---Fri Jul 21 14:42:08 2017 I am Thread-6 got locka---Fri Jul 21 14:42:08 2017 I am Thread-4 got lockb---Fri Jul 21 14:42:08 2017 I am Thread-4 got locka---Fri Jul 21 14:42:08 2017 I am Thread-7 got locka---Fri Jul 21 14:42:08 2017 I am Thread-7 got lockb---Fri Jul 21 14:42:08 2017 I am Thread-7 got lockb---Fri Jul 21 14:42:08 2017 I am Thread-7 got locka---Fri Jul 21 14:42:08 2017 I am Thread-9 got locka---Fri Jul 21 14:42:08 2017 I am Thread-9 got lockb---Fri Jul 21 14:42:08 2017 I am Thread-9 got lockb---Fri Jul 21 14:42:08 2017 I am Thread-9 got locka---Fri Jul 21 14:42:08 2017 I am Thread-5 got lockb---Fri Jul 21 14:42:08 2017 I am Thread-5 got locka---Fri Jul 21 14:42:08 2017 I am Thread-10 got locka---Fri Jul 21 14:42:08 2017 I am Thread-10 got lockb---Fri Jul 21 14:42:08 2017 I am Thread-10 got lockb---Fri Jul 21 14:42:08 2017 I am Thread-10 got locka---Fri Jul 21 14:42:08 2017 I am Thread-8 got locka---Fri Jul 21 14:42:08 2017 I am Thread-8 got lockb---Fri Jul 21 14:42:08 2017 I am Thread-8 got lockb---Fri Jul 21 14:42:08 2017 I am Thread-8 got locka---Fri Jul 21 14:42:08 2017