Python中使用Queue、Pipe怎么实现进程通信

发布时间:2021-06-12 17:50:39 作者:Leah
来源:亿速云 阅读:148

今天就跟大家聊聊有关Python中使用Queue、Pipe怎么实现进程通信,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

进程通信的概念


Queue:

Queue 在multiprocessing 模块中:from multiprocessing import Queue

#Queue在multiprocessing中
from multiprocessing import Queue,Process

def f(q):#要在主进程外使用,需要作为参数传入
  q.put(['helloworld'])
  

def m(q):
  print("get in p2:",q.get())

if __name__=="__main__":
  q=Queue()
  p=Process(target=f,args=(q,))
  p.start()
  p2=Process(target=m,args=(q,))
  p2.start()

Pipe:

Pipe 在multiprocessing 模块中:from multiprocessing import Pipe

from multiprocessing import Pipe,Process

def f(conn):
  a=[1,2,3,4]
  conn.send(a)
  conn.close()
def m(conn):
  a=conn.recv()
  conn.close()


if __name__=="__main__":
  parent_conn,child_conn=Pipe()#返回两个值,第一个只能发,第二个只能收
  p1=Process(target=f,args=(child_conn,))
  p2 = Process(target=m, args=(parent_conn,))#
  p1.start()
  p2.start()
  p1.join()
  p2.join()

看完上述内容,你们对Python中使用Queue、Pipe怎么实现进程通信有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

推荐阅读:
  1. python进程使用Queue和Pipe通信
  2. nodejs中stream和pipe机制怎么实现

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python queue

上一篇:Django中怎么对ModelForm进行操作

下一篇:微信小程序中怎么实现一个登录页面

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》