【基础】练习册54-Python3_进程2
进程2
代码如下:
# -*- coding: utf-8 -*-
# process_pipe.py
# @author Alyna
# @description
# @created 2020-05-12T15:21:45.616Z+08:00
# @last-modified 2020-05-12T16:24:54.157Z+08:00
#
from multiprocessing import Process,Pipe
'''
def send(pipe):
pipe.send(['spam',42,'egg']) #send传输一个列表
pipe.close()
if __name__=='__main__':
(con1,con2)=Pipe() #创建两个Pipe实例
sender = Process(target=send,args=(con1, ))
sender.start()
print("con2 got: %s"%con2.recv())#管道的另一端
con2.close()
'''
def talk(pipe):
pipe.send(dict(name='Bob',spam=42)) #传输一个字典
reply = pipe.recv() #接收传输的数据
print('talker got:',reply)
if __name__=='__main__':
(parentEnd,childEnd) = Pipe()
child = Process(target=talk,args=(childEnd,))
child.start()
print('parent got:',parentEnd.recv())
parentEnd.send({x*2 for x in 'spam'})
child.join()
print('parent exit')
运行结果为:
parent got: {'name': 'Bob', 'spam': 42}
talker got: {'ss', 'aa', 'mm', 'pp'}
parent exit