Pipe pipe in the Python multiprocessing module

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-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

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.