管道:双向通信 2个进程之间相互通信

 1 from multiprocessing import Pipe,Process
 2 
 3 def func(conn1,conn2):
 4     conn2.close()
 5     while True:
 6         try :
 7             msg = conn1.recv()
 8             print(msg)
 9         except EOFError:  #抛出无数据时异常
10             conn1.close()
11             break
12 
13 if __name__ == '__main__':
14     conn1, conn2 = Pipe()
15     Process(target=func,args = (conn1,conn2)).start()
16     conn1.close()
17     for i in range(20):
18         conn2.send('吃了么')
19     conn2.close()
实例

相关文章:

  • 2022-03-01
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-07
  • 2021-10-02
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-12-05
  • 2022-12-23
  • 2021-12-04
  • 2021-12-05
  • 2021-10-31
相关资源
相似解决方案