>>进程间通信Pipe
# Pipe 进程通信管道
'''
管道是系统实行通信的一种技术手段,开发者可以利用管道创建两个通信链接对象,这两个对象可以实现单端通信,也可以是实现双端通信
'''
import multiprocessing
def send_data(conn1,data):
conn1.send(data)
def recv_data(conn2):
print(conn2.recv())
def main():
(conn1,conn2) = multiprocessing.Pipe()
p1=multiprocessing.Process(target=send_data,args=(conn1,'ni shi yige da sha bi'))
p2=multiprocessing.Process(target=recv_data,args=(conn2,))
p1.start()
p2.start()
if __name__ == '__main__':
main()
# 使用进程通信实现生产者消费模型
import multiprocessing
import time
def producer(conn1,data):
for result_data in data:
conn1.send(result_data)
time.sleep(1)
def customer(conn2):
while True:
try:
print(conn2.recv())
except EOFError:
break
def main():
data1 = [i for i in 'nishiyigedashabi']
(conn1,conn2) = multiprocessing.Pipe()
p1=multiprocessing.Process(target=producer,args=(conn1,data1))
p2=multiprocessing.Process(target=customer,args=(conn2,))
p1.start()
p2.start()
conn1.close()
conn2.close()
if __name__ == '__main__':
main()