管道
from multiprocessing import Pipe,Process #不如队列安全
def func(conn1,conn2):
conn2.close() #在子进程关闭conn2
while True:
try:
msg=s=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() #在主进程关闭conn2,关闭后在读数据会抛出异常