Explanation of anonymous pipeline instance for Python process communication

Source: Internet
Author: User
This article mainly introduces the example of anonymous pipeline for Python process communication. This article provides code examples directly. the code contains detailed annotations. For more information, see Anonymous pipeline

The MPs queue is a one-way channel, which is similar to the shared memory cache. the MPs queue has two ends, including the input and output ends. for a process, it can only see one end of the pipeline, that is, either the input end or the output end.

OS. pipe () returns two file descriptors (r, w), which indicate readable and writable. the sample code is as follows:

The code is as follows:


#! /Usr/bin/python
Import time
Import OS

Def child (wpipe ):
Print ('Hello from child ', OS. getpid ())
While True:
Msg = 'How are you \ n'. encode ()
OS. write (wpipe, msg)
Time. sleep (1)

Def parent ():
Rpipe, wpipe = OS. pipe ()
Pid = OS. fork ()
If pid = 0:
Child (wpipe)
Assert False, 'fork child process error! '
Else:
OS. close (wpipe)
Print ('Hello from parent', OS. getpid (), pid)
Fobj = OS. fdopen (rpipe, 'r ')
While True:
Recv = OS. read (rpipe, 32)
Print recv

Parent ()


The output is as follows:

The code is as follows:


('Hello from parent', 5053,505 4)
('Hello from child ', 5054)
How are you

How are you

How are you

How are you

We can also improve the code without using the OS. read () reads binary bytes from the pipeline, but from the file object. in this case, OS is required. fdopen () encapsulates the underlying file descriptor (pipeline) into a file object, and then reads it using the readline () method in the file object. note that the readline () method of the file object always reads a line with the linefeed '\ n' and reads the line feed. another point to be improved is to close the unused end of the parent process and child process pipeline.

The code is as follows:


#! /Usr/bin/python
Import time
Import OS

Def child (wpipe ):
Print ('Hello from child ', OS. getpid ())
While True:
Msg = 'How are you \ n'. encode ()
OS. write (wpipe, msg)
Time. sleep (1)

Def parent ():
Rpipe, wpipe = OS. pipe ()
Pid = OS. fork ()
If pid = 0:
OS. close (rpipe)
Child (wpipe)
Assert False, 'fork child process error! '
Else:
OS. close (wpipe)
Print ('Hello from parent', OS. getpid (), pid)
Fobj = OS. fdopen (rpipe, 'r ')
While True:
Recv = fobj. readline () [:-1]
Print recv

Parent ()

The output is as follows:

The code is as follows:


('Hello from parent', 5108,510 9)
('Hello from child ', 5109)
How are you
How are you
How are you


If two-way communication is required with sub-processes, only one pipe pipeline is required. the following example creates two pipelines for the parent process and then fork the child process. OS. dup2 () redirects the output and input. the spawn function is similar to subprocess. popen (), which can send messages to sub-processes and obtain the returned data from the sub-processes.

The code is as follows:


#! /Usr/bin/python
# Coding = UTF-8
Import OS, sys

Def spawn (prog, * args ):
StdinFd = sys. stdin. fileno ()
StdoutFd = sys. stdout. fileno ()

ParentStdin, childStdout = OS. pipe ()
ChildStdin, parentStdout = OS. pipe ()

Pid = OS. fork ()
If pid:
OS. close (childStdin)
OS. close (childStdout)
OS. dup2 (parentStdin, stdinFd) # bind the input stream to the MPs queue and redirect the input to parentStdin at one end of the MPs queue.
OS. dup2 (parentStdout, stdoutFd) # bind the output stream to the MPs queue and send it to the child process childStdin.
Else:
OS. close (parentStdin)
OS. close (parentStdout)
OS. dup2 (childStdin, stdinFd) # bind the input stream to the MPs queue
OS. dup2 (childStdout, stdoutFd)
Args = (prog,) + args
OS .exe cvp (prog, args)
Assert False, 'execvp failed! '

If _ name _ = '_ main __':
Mypid = OS. getpid ()
Spawn ('Python', 'pipelest. py', 'spam ')

Print 'Hello 1 from parent', mypid # print to the output stream parentStdout, which is sent to the child process childStdin through the pipeline
Sys. stdout. flush ()
Reply = raw_input ()
Sys. stderr. write ('parent got: "% s" \ n' % reply) # stderr is not bound to the MPs queue.

Print 'Hello 2 from parent', mypid
Sys. stdout. flush ()
Reply = sys. stdin. readline () # obtain the information returned by the sub-process in another way.
Sys. stderr. write ('parent got: "% s" \ n' % reply [:-1])

The pipetest. py code is as follows:

The code is as follows:


# Coding = UTF-8
Import OS, time, sys

Mypid = OS. getpid ()
Parentpid = OS. getppid ()
Sys. stderr. write ('child % d of % d got arg: "% s" \ n' % (mypid, parentpid, sys. argv [1])

For I in range (2 ):
Time. sleep (3)
Recv = raw_input () # obtain data from the MPs queue. the Source is stdout.
Time. sleep (3)
Send = 'child % d got: [% s] '% (mypid, recv)
Print (send) # stdout is bound to the MPs queue and sent to the parent process stdin.
Sys. stdout. flush ()

Output:

The code is as follows:


Child 7265 of 7264 got arg: "spam"
Parent got: "Child 7265 got: [Hello 1 from parent 7264]"
Parent got: "Child 7265 got: [Hello 2 from parent 7264]"

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.