from multiprocessing import Process,Pipe
      import os,time
      # fd1只能recv,fd2只能send
      # fd1,fd2 = Pipe(False)
      # 创建一个双向管道
      fd1,fd2 = Pipe()
      # fd1.close()

      def fun(name):
          time.sleep(1)
          # 子进程发送字符串到管道
          fd2.send("hello "+str(name))
          print(os.getppid(),"...",os.getpid())
      jobs = []
      for i in range(5):
          p = Process(target = fun,args = (i,))
          jobs.append(p)
          p.start()
      # 父进程从管道接受子进程发送来的消息,发送与接受的都是字符串
      for i in range(5):
          data = fd1.recv()
          print(data)
      for i in jobs:
          i.join()

  

相关文章:

  • 2021-08-24
  • 2021-11-23
  • 2022-01-23
  • 2022-12-23
  • 2022-01-14
  • 2022-12-23
  • 2021-07-08
  • 2021-10-06
猜你喜欢
  • 2022-02-27
  • 2022-01-16
  • 2021-06-10
  • 2021-10-21
相关资源
相似解决方案