网站首页  汉语字词  英语词汇  考试资料  写作素材  旧版资料

请输入您要查询的考试资料:

 

标题 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, 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
随便看

 

在线学习网考试资料包含高考、自考、专升本考试、人事考试、公务员考试、大学生村官考试、特岗教师招聘考试、事业单位招聘考试、企业人才招聘、银行招聘、教师招聘、农村信用社招聘、各类资格证书考试等各类考试资料。

 

Copyright © 2002-2024 cuapp.net All Rights Reserved
更新时间:2025/5/16 20:10:26