进程间协同
进程间写操作需要加锁,需要处理进程间协同的问题。
进程间协同的方式:Queue、Lock、Semaphore、Event、Pipe、Condition。
1、Queue -->put()
get()
empty()
JoinableQueue-->task.done()
join()
----------------------------------------------------------------------------------------------------------------
2、Lock -->acquire()#申请锁
release()#释放锁
---------------------------------------------------------------------------------------------------
练习1:按顺序打印
# 没有加锁
def func(num):
time.sleep(0.1)
print('hello Num:%s'%(num))
if __name__ == '__main__':
for num in range(50):
Process(target=func,args=(num,)).start()
# 执行结果
hello Num:6
hello Num:21
hello Num:42
hello Num:17
hello Num:2
hello Num:10
hello Num:3
hello Num:7
hello Num:14
hello Num:18
#加锁后按顺序打印
from multiprocessing import Process,Value,Lock
import time
def func(num,lock):
lock.acquire()
time.sleep(0.1)
print('hello Num:%s'%(num))#按顺序打印
num.value +=1
lock.release()
if __name__ =='__main__':
lock=Lock()
val = Value('i',0)
procs =[Process(target=func,args=(val,lock)) for i in range(50)]
for p in procs:
p.start()
for p in procs:
p.join()
print(val.value)
#执行结果
hello Num:<Synchronized wrapper for c_long(42)>
hello Num:<Synchronized wrapper for c_long(43)>
hello Num:<Synchronized wrapper for c_long(44)>
hello Num:<Synchronized wrapper for c_long(45)>
hello Num:<Synchronized wrapper for c_long(46)>
hello Num:<Synchronized wrapper for c_long(47)>
hello Num:<Synchronized wrapper for c_long(48)>
hello Num:<Synchronized wrapper for c_long(49)>
50
# 练习:修改Count类,使用lock,保证写加了锁
from multiprocessing import Process,Value,Lock
import time
class Count(object):
def __init__(self,count):
self.count = Value('d',count)
def increment(self,lock):
lock.acquire()
self.count.value +=1
lock.release()
def getValue(self):
return self.count.value
if __name__ == '__main__':
c1=Count(5)
lock =Lock()
processList = []
for i in range(50):
processList.append(Process(target=c1.increment,args=(lock,)))
for i in range(50):
processList[i].start()
for i in range(50):
processList[i].join()
print(c1.getValue())
# 修改代码使其按顺序打印
from multiprocessing import Process,Value,Lock
import time
def l(lock,num):
lock.acquire()
time.sleep(0.1)
print('hello Num:%s'%(num))#按顺序打印
lock.release()
if __name__ =='__main__':
lock=Lock()
for num in range(20):
Process(target= l,args=(lock,num)).start()
#执行结果
hello Num:17
hello Num:13
hello Num:19
hello Num:9
hello Num:10
hello Num:6
hello Num:8
hello Num:18
hello Num:11
hello Num:14
#修改后
def l(lock,num):
lock.acquire()
# time.sleep(0.1)
print('hello Num:%s'%(num))#按顺序打印
lock.release()
if __name__ =='__main__':
lock=Lock()
for num in range(20):
Process(target= l,args=(lock,num)).start()
time.sleep(0.1)
3、Semaphore -->acquire()
release()
--------------------------------------------------------------------------
4、Event --> is_set()
set()
wait([time])
clear()
-----------------------------------------------------------------------------------------------
5、Pipe -->pipe = mp.Pipe()
pipe[0].send()~pipe[1].recv()
pipe[1].send()~ pipe[0].recv()
----------------------------------------------------------------------------
6、Condition