# 管道
from multiprocessing import Pipe,Process def func(conn1,conn2): conn2.close() #子进程只关闭conn2时会抛出一个EOFError(没数据可取时recv),根据EOFError结束循环 while True: try : msg = conn1.recv()#不判断话会阻塞 print(msg) except EOFError: conn1.close() break if __name__ == ‘__main__‘: conn1, conn2 = Pipe() Process(target=func,args = (conn1,conn2)).start() conn1.close() for i in range(20): conn2.send(‘吃了么‘) conn2.close() #主进程Conn1和Conn2全部关闭,不会影响子进程的Conn1和Conn2
管道实现生产者消费者模型
# from multiprocessing import Lock,Pipe,Process# def producer(con,pro,name,food):# con.close()# for i in range(100):# f = ‘%s生产%s%s‘%(name,food,i)# print(f)# pro.send(f)# pro.send(None)# pro.send(None)# pro.send(None)# pro.close()## def consumer(con,pro,name,lock):# pro.close()# while True:# lock.acquire()# food = con.recv()# lock.release()# if food is None:# con.close()# break# print(‘%s吃了%s‘ % (name, food))# if __name__ == ‘__main__‘:# con,pro = Pipe()# lock= Lock()# p = Process(target=producer,args=(con,pro,‘egon‘,‘泔水‘))# c1 = Process(target=consumer, args=(con, pro, ‘alex‘,lock))# c2 = Process(target=consumer, args=(con, pro, ‘bossjin‘,lock))# c3 = Process(target=consumer, args=(con, pro, ‘wusir‘,lock))# c1.start()# c2.start()# c3.start()# p.start()# con.close()# pro.close() # from multiprocessing import Process,Pipe,Lock## def consumer(produce, consume,name,lock):# produce.close()# while True:# lock.acquire()# baozi=consume.recv()# lock.release()# if baozi:# print(‘%s 收到包子:%s‘ %(name,baozi))# else:# consume.close()# break## def producer(produce, consume,n):# consume.close()# for i in range(n):# produce.send(i)# produce.send(None)# produce.send(None)# produce.close()## if __name__ == ‘__main__‘:# produce,consume=Pipe()# lock = Lock()# c1=Process(target=consumer,args=(produce,consume,‘c1‘,lock))# c2=Process(target=consumer,args=(produce,consume,‘c2‘,lock))# p1=Process(target=producer,args=(produce,consume,30))# c1.start()# c2.start()# p1.start()# produce.close()# consume.close() # pipe 数据不安全性# IPC# 加锁来控制操作管道的行为 来避免(多人消费同时取一个数据)进程之间争抢数据造成的数据不安全现象 # 队列 进程之间数据安全的# 管道 + 锁 # 进程之间的数据共享
普通正常的进程之间数据是不共享的# from multiprocessing import Manager,Process # def main(dic):# dic[‘count‘] -= 1# print(dic)## if __name__ == ‘__main__‘:# m = Manager()# dic=m.dict({‘count‘:100})# p_lst = []# p = Process(target=main, args=(dic,))# p.start()# p.join() from multiprocessing import Manager,Process,Lockdef main(dic,lock): lock.acquire() dic[‘count‘] -= 1 #这里会出现两个进程同时修改一个数据情况,最后会发现每次次执行count的值不一样,就是因为多进程同时操作一个数据导致的 lock.release() #,正常理想count值应该是50,为了达到这效果就加锁。虽然加锁在效率会降低,但安全 if __name__ == ‘__main__‘: m = Manager() l = Lock() dic=m.dict({‘count‘:100}) p_lst = [] for i in range(50): p = Process(target=main,args=(dic,l)) p.start() p_lst.append(p) for i in p_lst: i.join() print(‘主进程‘,dic)
# 进程池1
原文地址:https://www.cnblogs.com/mys6/p/10850226.html
时间: 2024-10-01 07:26:29