The pipe pipe of the multiprocessing module in Python

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.