How Python implements the pipeline

Source: Internet
Author: User
Tags stdin

The example in this article describes how Python implements the process of piping. Share to everyone for your reference. The specific analysis is as follows:

What is the most dazzling magic that can be performed under Linux? believe that different people say different, but if there is no pipeline, then I am afraid that the magic will lose the magic

Here's how to use Python to handle these pipes.

Pipe Call subroutine

We want to use a subroutine in the program, but need dynamic transfer parameters (here is the dynamic, refers to according to the second-son program input results to determine what this input), how to do, do not panic, there are subprocess!

Below I will first introduce an example code, as well as his output results!

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14-15 16 #!/usr/bin/python from subprocess Import * subprocess management, can do a lot of subprocess files, we want to introduce this file P = Popen (["Cat", "n"], bufsize= 1024,stdin=pipe, Stdout=pipe, close_fds=true) # Open Program The first argument is a list (program name, parameter) # The second parameter is the buffer size # Stdin,stdout is setting whether to open these pipes, If his value is subprocess. Pipe the words, # will open, same as stdin and stderr # Close_fds set to True (unix-only) all file descriptors are closed (FIN, 0,1,2) except before subroutine execution (Fout, P.stdout) for I in Range (10): # You'll understand. ^_^ Fin.write ("line" + str (i)) fin.write (' n ') Fin.flush () print fout.readline ()

The result of his output is:

?

1 2 3 4 5 6 7 8 9 10 Line0 line1 line2 line3 line4 line5 line6 line7 line8 line9

I hope this article will help you with your Python programming.

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.