死锁和递归锁
1.死锁和递归锁
所谓死锁:是指两个或两个以上的进程或线程在执行过程中,因争夺资源而造成的一种互相等待的现象,若无外力作用,它们都将无法推进下去。此时称系统处于死锁状态或系统产生了死锁,这些永远在互相等待的进程称为死锁进程
from threading import Thread,Lock
import time
lock1=Lock()
lock2=lock1
def eat1(name):
lock1.acquire()
print('%s抓住了红buff' %name)
time.sleep(1)
lock2.acquire()
print('%s抓住了蓝buff' %name)
time.sleep(1)
lock2.release()
print('%s杀死了蓝buff' %name)
time.sleep(1)
lock1.release()
print('%s杀死了红buff' %name)
def eat2(name):
lock2.acquire()
print('%s抓住了蓝buff' %name)
time.sleep(1)
lock1.acquire()
print('%s抓住了红buff' %name)
time.sleep(1)
lock1.release()
print('%s杀死了红buff' %name)
time.sleep(1)
lock2.release()
print('%s杀死了蓝buff' %name)
if __name__ == '__main__':
for name in ['盖伦','亚索','赵信']:
t=Thread(target=eat1,args=(name,))
t.start()
for name in ['迪迦', '泰罗', '奈克瑟斯']:
t1 = Thread(target=eat2, args=(name,))
t1.start()
盖伦抓住了红buff
from threading import Thread,Lock,RLock
import time
lock1=RLock()
lock2=lock1
def eat1(name):
lock1.acquire()
print('%s抓住了红buff' %name)
time.sleep(1)
lock2.acquire()
print('%s抓住了蓝buff' %name)
time.sleep(1)
lock2.release()
print('%s杀死了蓝buff' %name)
time.sleep(1)
lock1.release()
print('%s杀死了红buff' %name)
def eat2(name):
lock2.acquire()
print('%s抓住了蓝buff' %name)
time.sleep(1)
lock1.acquire()
print('%s抓住了红buff' %name)
time.sleep(1)
lock1.release()
print('%s杀死了红buff' %name)
time.sleep(1)
lock2.release()
print('%s杀死了蓝buff' %name)
if __name__ == '__main__':
for name in ['盖伦','亚索','赵信']:
t=Thread(target=eat1,args=(name,))
t.start()
for name in ['迪迦', '泰罗', '奈克瑟斯']:
t1 = Thread(target=eat2, args=(name,))
t1.start()
2.线程队列
queue队列:使用import queue,用法与进程Queue一样 小queue
同一个进程下多个线程数据是共享的
为什么先同一个进程下还会去使用队列呢
因为队列是
管道 + 锁
所以用队列还是为了保证数据的安全
import queue
q=queue.Queue()
q.put('first')
q.put('second')
q.put('third')
print(q.get())
print(q.get())
print(q.get())
'''
结果(先进先出):
first
second
third
'''
q=queue.LifoQueue()
q.put('first')
q.put('second')
q.put('third')
print(q.get())
print(q.get())
print(q.get())
'''
结果(后进先出):
third
second
first
'''
import queue
q=queue.PriorityQueue()
q.put((20,'a'))
q.put((10,'b'))
q.put((10,'b1'))
q.put((30,'c'))
print(q.get())
print(q.get())
print(q.get())
'''
结果(数字越小优先级越高,优先级高的优先出队):
(10, 'b')
(10, 'b1')
(20, 'a')
(30, 'c')
'''
GIL不是python特点而是cpython解释器特点
(******)GIL会导致同一个进程下的多个线程无法同时执行,无法利用多核优势
3.concurrent模块 进程池和线程池
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor
def task(n, m):
return n+m
def coll_back(res):
print(res.result())
if __name__ == '__main__':
p_pool = ProcessPoolExecutor(3)
for i in range(1,12):
p_pool.submit(task, m=1, n=2).add_done_callback(coll_back)
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor
def task(n, m):
return n+m
def coll_back(res):
print(res.result())
if __name__ == '__main__':
p_pool = ProcessPoolExecutor(3)
for i in range(1,12):
p_pool.submit(task, m=1, n=2).add_done_callback(coll_back)
4.协程基础
协程:是单线程下的并发,又称微线程,纤程。英文名Coroutine。一句话说明什么是协程:协程是一种用户态的轻量级线程,即协程是由用户程序自己控制调度的。
python的线程属于内核级别的,即由操作系统控制调度(如单线程遇到io或执行时间过长就会被迫交出cpu执行权限,切换其他线程运行)
优点:
协程的切换开销更小,属于程序级别的切换,操作系统完全感知不到,因而更加轻量级
单线程内就可以实现并发的效果,最大限度地利用cpu
缺点如下:
协程的本质是单线程下,无法利用多核,可以是一个程序开启多个进程,每个进程内开启多个线程,每个线程内开启协程
协程指的是单个线程,因而一旦协程出现阻塞,将会阻塞整个线程
总结协程特点:
必须在只有一个单线程里实现并发
修改共享数据不需加锁
用户程序里自己保存多个控制流的上下文栈
附加:一个协程遇到IO操作自动切换到其它协程(如何实现检测IO,yield、greenlet都无法实现,就用到了gevent模块(select机制))
5. 协程之greenlet模块
from greenlet import greenlet
def eat(name):
print('%s吃了一会'%name)
g2.switch(name)
print('%s又吃了一会' % name)
g2.switch()
def play(name):
print('%s玩了一会'%name)
g1.switch()
print('%s又玩了一会' % name)
g1=greenlet(eat)
g2=greenlet(play)
g1.switch('z')
6. 协程之gevent模块
import gevent
def eat(name):
print('%s eat 1' %name)
gevent.sleep(2)
print('%s eat 2' %name)
def play(name):
print('%s play 1' %name)
gevent.sleep(1)
print('%s play 2' %name)
g1=gevent.spawn(eat,'z')
g2=gevent.spawn(play,name='z')
g1.join()
g2.join()