Python implements a method for processing pipelines

Source: Internet
Author: User
The examples in this article describe how Python implements the processing pipeline. 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 pipe, then I am afraid that the magic will be lost magic

Here's how to use Python to handle these pipelines

Pipeline Call subroutine

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

Let me start with an example code and his output!

#!/usr/bin/pythonfrom subprocess Import *# subprocess Management, can do a lot of sub-process files, we want to introduce this file P = Popen (["Cat", "-N"], bufsize= 1024,stdin=pipe, Stdout=pipe, close_fds=true) # Open Program The first parameter is a list (program name, parameter) # The second parameter is the buffer size # Stdin,stdout is the setting whether to open these pipes, If his value is subprocess. Pipe, # will open, same as stdin and stderr# Close_fds set to True (unix-only) all file descriptors except (0,1,2) are closed before subroutine execution (FIN, fout) = (P.stdin, 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:

Line0line1line2line3line4line5line6line7line8line9

Hopefully this article will help you with Python programming.

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