python 多进程 管道(并行编程 16)

2019-07-30 11:11:54 浏览数 (3)

import multiprocessing

def initem(pipe): out,=pipe for item in range(10): out.send(item) out.close() def mulitem(pipe1,pipe2): close,inpipe=pipe1 close.close() out,=pipe2 try: while True: item=inpipe.recv() out.send(item*item) except: out.close() if name=="main": pipe1=multiprocessing.Pipe(True) pipe2=multiprocessing.Pipe(True) p1=multiprocessing.Process(target=initem,args=(pipe1,)) p1.start() p2=multiprocessing.Process(target=mulitem,args=(pipe1,pipe2)) p2.start() pipe1[0].close() pipe2[0].close() try: while True: print(pipe2[1].recv()) except: print("End")

管道里,输出的值给输入

0 人点赞