zoukankan      html  css  js  c++  java
  • 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()
    

      

  • 相关阅读:
    活动安排
    中国剩余定理
    欧拉回路
    单词游戏
    Ant Trip
    John's Trip
    太鼓达人
    相框
    原始生物
    Blockade
  • 原文地址:https://www.cnblogs.com/liuhaidon/p/12432901.html
Copyright © 2011-2022 走看看