Multiprocessing. Pipe ([duplex])
returns 2 Connection objects (CONN1, CONN2), representing the ends of the pipe, which are two-way traffic. If DUPLEX=FALSE,CONN1 can only be used to receive messages, CONN2 can only send messages. The Os.open is different from os.pipe () Returns 2 file descriptors (R, W) that represent both readable and writable
Examples are as follows:
Copy Code code 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:
Copy Code code as follows:
Con2 got: [' spam ', +, ' egg ']
(' Parent got: ', {' name ': ' Bob ', ' spam ': 42} ')
(' Talker Got: ', set ([' SS ', ' AA ', ' pp ', ' mm '])
Parent exit