时间:2021-07-01 10:21:17 帮助过:258人阅读
实例如下:
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')
输出如下: