The Pipe pipe in the Python multiprocessing module USES the instance
- 2020-05-09 18:47:28
- OfStack
multiprocessing.Pipe([duplex])
Returns two connection objects (conn1, conn2), representing both ends of the pipe, and the default is two-way communication. If duplex=False,conn1 can only be used to receive messages and conn2 can only be used to send messages
Examples are as follows:
#!/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()# from send Messages are received
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')
The output is as follows:
con2 got: ['spam', 42, 'egg']
('parent got:', {'name': 'Bob', 'spam': 42})
('talker got:', set(['ss', 'aa', 'pp', 'mm']))
parent exit