欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python multiprocessing模塊中的Pipe管道使用實(shí)例

 更新時間:2015年04月11日 11:30:21   投稿:junjie  
這篇文章主要介紹了Python multiprocessing模塊中的Pipe管道使用實(shí)例,本文直接給出使用實(shí)例,需要的朋友可以參考下

multiprocessing.Pipe([duplex])
返回2個連接對象(conn1, conn2),代表管道的兩端,默認(rèn)是雙向通信.如果duplex=False,conn1只能用來接收消息,conn2只能用來發(fā)送消息.不同于os.open之處在于os.pipe()返回2個文件描述符(r, w),表示可讀的和可寫的

實(shí)例如下:

復(fù)制代碼 代碼如下:

#!/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')

輸出如下:

復(fù)制代碼 代碼如下:

con2 got: ['spam', 42, 'egg']
('parent got:', {'name': 'Bob', 'spam': 42})
('talker got:', set(['ss', 'aa', 'pp', 'mm']))
parent exit

相關(guān)文章

最新評論