Pipe管道:

* 管道实例化后会产生两个通道,分别交给两个进程
* 通过send和recv来交互数据,这是一个双向的管道,child和parent可以互相收发

from multiprocessing import Process, Pipe

def f(conn):
    conn.send([42, None, 'hello'])
    conn.send([43,32])
    print(conn.recv())
    conn.close()

if __name__ == '__main__':
    #Pipe实例化返回一个元祖对象,分别给到主进程端口,子进程端口
    parent_conn, child_conn = Pipe()
    #其中1个端口对象作为参数传入至进程
    p = Process(target=f, args=(child_conn,))
    p.start()
    #数据传递,也是以队列的形式进行传递,先发送先接受。
    print(parent_conn.recv())  # prints "[42, None, 'hello']"
    print(parent_conn.recv())
#     parent_conn.send('主进程发送给子进程的数据')
    p.join()

 

相关文章:

  • 2021-12-05
  • 2021-12-05
  • 2021-06-01
  • 2021-06-25
  • 2021-09-27
  • 2021-12-10
  • 2022-12-23
  • 2021-06-10
猜你喜欢
  • 2021-11-27
  • 2022-12-23
  • 2021-11-24
  • 2021-11-24
  • 2021-12-04
  • 2021-04-18
  • 2021-12-05
相关资源
相似解决方案