Python multiprocessing模块中的Pipe管道使用实例
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, Pipedef 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
您可能感兴趣的文章:
- Linux管道pipe使用实例
- 13-(1-4)进程管道关于popen(-r-w)及pipe的程序使用实例
- 无名管道(pipe)使用实例
- 《Lua程序设计》9.2 管道(pipe)与过滤器(filter) 包含使用协同函数实现“生产者——消费者”问题的实例代码
- 实例——Linux管道pipe的使用
- 管道pipe与dup结合使用,应用实例
- 使用无名管道pipe实现进程间通信
- angular2--pipe管道使用
- python使用marshal模块序列化实例
- Linux下用fork()派生的子进程通过pipe管道通讯的实例详解("生产者-消费者"问题)
- linux shell 管道命令(pipe)使用及与shell重定向区别
- 在.NET程序中使用PIPE(管道技术)
- 用管道(pipe)使消息队列通知(mq_notify)可以在Select和poll中使用
- linux shell 管道命令(pipe)使用及与shell重定向区别
- Python 3.x--使用re模块,实现计算器运算实例
- Linux系统编程:pipe匿名管道的使用,实现linux命令下管道命令
- Python使用email模块对邮件进行编码和解码的实例教程
- python模块restful使用方法实例
- python time模块使用实例
- Linux环境编程--如何用C语言创建多进程运行以及简单的pipe管道使用