from multiprocessing import Process ,Pipe
def fun(conn1):
while True:
msg=conn1.recv()
if msg is None:break
print(msg)
if __name__=="__main__":
conn1,conn2=Pipe()
Process(target=fun,args=(conn1,)).start()
for i in range(20):
conn2.send('很爱很爱你')
conn2.send(None)
改进的代码
from multiprocessing import Process ,Pipe
def fun(conn1,conn2):
conn2.close()
while True:
try:
msg=conn1.recv()
print(msg)
except EOFError:
conn1.close()
break
if __name__=="__main__":
conn1,conn2=Pipe()
Process(target=fun,args=(conn1,conn2)).start()
conn1.close()
for i in range(20):
conn2.send('很爱很爱你')
conn2.close()
管道
最新推荐文章于 2023-12-26 16:35:35 发布