Multiprocessing. Pipe ([duplex])
Returns 2 Connection objects (CONN1, conn2), which represent both ends of the pipeline, and the default is two-way communication. If DUPLEX=FALSE,CONN1 can only be used to receive messages, CONN2 can only be used to send messages. Unlike Os.open, where Os.pipe () Returns 2 file descriptors (R, W) representing both readable and writable
Examples are as follows:
#!/usr/bin/python
#coding =utf-8
Import OS
From multiprocessing import Process, Pipe
def send (pipe):
Pipe.send ([' spam ') + [[] [' 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 ')
The output is as follows:
Con2 got: [' spam ', A, ' egg ']
(' Parent got: ', {' name ': ' Bob ', ' spam ': 42})
(' Talker Got: ', set ([' SS ', ' AA ', ' pp ', ' mm '])
Parent exit
Codego.net code excerpt.
Pipe pipe in the Python multiprocessing module