Python multiprocessing模块中的Pipe管道使用实例
作者:junjie 时间:2023-08-18 10:01:31
multiprocessing.Pipe([duplex])
返回2个连接对象(conn1, conn2),代表管道的两端,默认是双向通信.如果duplex=False,conn1只能用来接收消息,conn2只能用来发送消息.不同于os.open之处在于os.pipe()返回2个文件描述符(r, w),表示可读的和可写的
实例如下:
#!/usr/bin/python
#coding=utf-8
import os
from multiprocessing import Process, Pipe
def send(pipe):
pipe.send(['spam'] + [42, 'egg'])
pipe.close()
def talk(pipe):
pipe.send(dict(name = 'Bob', spam = 42))
reply = pipe.recv()
print('talker got:', reply)
if __name__ == '__main__':
(con1, con2) = Pipe()
sender = Process(target = send, name = 'send', args = (con1, ))
sender.start()
print "con2 got: %s" % con2.recv()#从send收到消息
con2.close()
(parentEnd, childEnd) = Pipe()
child = Process(target = talk, name = 'talk', args = (childEnd,))
child.start()
print('parent got:', parentEnd.recv())
parentEnd.send({x * 2 for x in 'spam'})
child.join()
print('parent exit')
输出如下:
con2 got: ['spam', 42, 'egg']
('parent got:', {'name': 'Bob', 'spam': 42})
('talker got:', set(['ss', 'aa', 'pp', 'mm']))
parent exit
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
OpenCV图像处理GUI功能详解
![](https://img.aspxhome.com/file/2023/4/76754_0s.png)
用Python实现最速下降法求极值的方法
![](https://img.aspxhome.com/file/2023/7/112977_0s.jpg)
Oracle 安装和卸载问题收集(集合篇)第1/6页
粗暴解决CUDA out of memory的问题
Java正则表达式匹配字符串并提取中间值的方法实例
Python字符串的字符转换、字符串劈分、字符串合并问题分析
![](https://img.aspxhome.com/file/2023/0/72960_0s.png)
Python读取系统文件夹内所有文件并统计数量的方法
分享13个非常有用的Python代码片段
Makefile构建Golang项目示例详解
ServerXMLHTTP的setTimeouts超时设置
详解如何使用vue-cli脚手架搭建Vue.js项目
![](https://img.aspxhome.com/file/2023/1/139921_0s.png)
基于Python搭建人脸识别考勤系统
![](https://img.aspxhome.com/file/2023/9/78759_0s.jpg)
小谈访客浏览器分辨率
![](https://img.aspxhome.com/file/UploadPic/200710/18/20071018132343756s.png)
Pandas实现数据拼接的操作方法详解
![](https://img.aspxhome.com/file/2023/3/105883_0s.png)
Git的撤销、修改和回退命令
Python反爬虫技术之防止IP地址被封杀的讲解
![](https://img.aspxhome.com/file/2023/3/108123_0s.png)
pygame游戏之旅 载入小车图片、更新窗口
![](https://img.aspxhome.com/file/2023/5/86425_0s.png)
python批量修改文件后缀示例代码分享
解析一个通过添加本地分区索引提高SQL性能的案例
Windows10下安装解压版MySQL教程图文详解
![](https://img.aspxhome.com/file/2023/4/80004_0s.png)