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-8ImportOs fromMultiprocessingImportProcess, Pipe def send(pipe):Pipe.send ([' spam '] + [ the,' Egg ']) Pipe.close () def talk(pipe):Pipe.send (dict (name =' Bob ', Spam = the)) 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 forXinch ' 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‘exit
The pipe pipe of the multiprocessing module in Python