Using pipe to manipulate Linux pipelines in Python _python

Source: Internet
Author: User
Tags readline stdin in python

In Linux, processes are communicated in the form of signals, pipelines, shared memory, Message Queuing sockets, and so on. Where pipelines are the oldest form of communication between *nix systems, all *nix provide this type of communication. A pipe is a half-duplex communication mechanism, that is, it can only be read at one end and the other end to write; In addition, pipelines can only be used to communicate between two processes that have a common ancestor. Pipeline communication follows the principle of FIFO, and the data can only be read once, and it is important that the data disappear from the data as soon as it is read.

On Linux, creating a pipeline uses the pipe function, which, when executed, produces two file descriptors, respectively, for the read and write ends. Pipelines in a single process have little effect, typically calling pipe and then calling fork to create an IPC channel from the parent process to the child process.

Linux, we often use the pipeline, such as the Cat command to view a large file, a page can not be all displayed, we can through the Cat xxx | More to page display, or search file content can use Cat XXX | grep search to do, where we all use the pipe. Next, I'll write a piece of automatic page-display program in Python instead of using the pipe manually.

#!/usr/bin/env python
import os,sys
if not sys.argv[1:]:
  print "No filename input"
  sys.exit (1)
Try:
    fp = open (Sys.argv[1], "R")
except ioerror,msg:
  sys.exit (msg)
pi=os.pipe ()
pid=os.fork ()
if PID:
  #parent
  os.close (pi[0]) #close Read pipe
  #write to Pipe
  line=fp.readline ()
  While line:
    os.write (pi[1],line)
    line=fp.readline ()
  #close Write pipe
  os.close (pi[1))
  # Wait for Chile
  os.waitpid (pid,0)
else:
  os.close (pi[1]) #close write pipe
  #put pipe Read to stdin< C26/>os.dup2 (Pi[0],sys.stdin.fileno ())
  Os.close (pi[0])
  os.execl ("/bin/more", "more")

Save this code as scat.py, add execute permissions, run scat.py file name, the system will automatically read the contents of the file and paging, and the use of cat file name | The effect is exactly the same. In the code above, the FORK,DUP2 and Exec series functions mentioned in the previous blogs are used.

The first is that the program creates a pipe, after the system fork, the parent process closes its read end, the child process closes its write end, the parent process reads the file name passed, and writes the contents through the pipe write port to the pipe, closes the write port, and waits for the end of the child process. After the child process closes the write port, the read port is redirected to the standard input of the process, the child process can automatically receive the data passed by the pipeline, and finally uses the EXECL function to call the system's more program to process the passed data, so as to easily achieve the paging effect.

Pipe is a half-duplex communication mechanism, and if you want to use full-duplex communication between processes, you can create two of pipes to achieve full-duplex effects. In addition, the pipe anonymous pipeline can only be used to communicate between processes that have the same parent process, and *nix provides another FIFO (named pipe) that enables communication between any process, in the next blog.

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.