7.18 python进程间数据共享
# 管道
# 数据共享 Manager
# 进程池和回调函数 !
# !/usr/bin/env python # !--*--coding:utf-8 --*-- # !@Time :2018/7/18 14:23 # !@Author TrueNewBee # 管道 实现两进程间的信息传递 from multiprocessing import Pipe, Process # 一种使用管道方式 def func(conn3, conn4): conn4.close() # 把多余通道关闭 while True: try: msg = conn3.recv() # 接收消息 print(msg) except EOFError: # 没有数据可以取的时候抛出异常 conn3.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()
# !/usr/bin/env python # !--*--coding:utf-8 --*-- # !@Time :2018/7/18 15:01 # !@Author TrueNewBee # pipe 数据不安全性 这是一个例子,下面有另外一个解决方案的代码加上锁! # IPC import time import random from multiprocessing import Process, Pipe def producer(con1, pro1, name, food): """通过管道把生产东西传给消费者""" con1.close() for i in range(4): time.sleep(random.randint(1, 3)) f = '%s生产%s%s' % (name, food, i) print(f) pro1.send(f) pro1.close() def consumer(con2, pro2, name1): pro2.close() while True: try: food = con2.recv() print('%s吃了%s' % (name1, food)) time.sleep(random.randint(1, 3)) except EOFError: con2.close() break if __name__ == '__main__': con, pro = Pipe() p = Process(target=producer, args=(con, pro, 'a', '泔水')) c1 = Process(target=consumer, args=(con, pro, 'b')) c2 = Process(target=consumer, args=(con, pro, 'c')) c3 = Process(target=consumer, args=(con, pro, 'd')) p.start() c1.start() c2.start() c3.start() con.close() pro.close() # 加上锁解决pipe数据不安全问题 # from multiprocessing import Process, Pipe, Lock # # # def consumer(p, name, lock1): # produce1, consume1 = p # produce1.close() # while True: # lock1.acquire() # food=consume1.recv() # lock.release() # if food: # print('%s 收到包子:%s' %(name,baozi)) # else: # consume1.close() # break # # # def producer(p, n): # produce2, consume2=p # consume2.close() # for i in range(n): # produce2.send(i) # produce2.send(None) # produce2.send(None) # produce2.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), 10)) # c1.start() # c2.start() # p1.start() # # produce.close() # consume.close() # # c1.join() # c2.join() # p1.join() # print('主进程') # 加锁来控制操作管道的行为 来避免进程之间争抢数据造成的数据不安全现象 # 队列 进程之间数据安全的 # 管道 + 锁
# !/usr/bin/env python # !--*--coding:utf-8 --*-- # !@Time :2018/7/18 15:51 # !@Author TrueNewBee # 牺牲效率 保障了数据安全 from multiprocessing import Manager, Process, Lock def main(dic1, lock1): lock1.acquire() # 加上所也是为了数据不安全问题 dic1['count'] -= 1 lock1.release() if __name__ == '__main__': m = Manager() lock = Lock() dic = m.dict({'count': 100}) p_list = [] for i in range(50): p = Process(target=main, args=(dic, lock)) p.start() p_list.append(p) for i in p_list: p.join() print('主进程:', dic)
管道介绍:
真正在用的时候,底层东西不用,都是拿来主义,面试造核弹,工作拧螺丝!!!