Python—进程间通信


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()

  


作者:刘_love_田,发布于:2020/03/07
原文:https://www.cnblogs.com/liuhaidon/p/12432901.html