A detailed introduction to the Python standard library subprocess subprocess Package

Source: Internet
Author: User

The content here is based on the Linux Process Foundation and the Linux text stream. The main function of the subprocess package is to execute external commands and programs. For example, I need to use wget to download files. I called the wget program in Python. In this sense, the subprocess function is similar to the shell.

Subprocess and common encapsulation functions

When we run Python, we are all creating and running a process. As we described in the Linux Process Foundation, a process can fork a child process and let the child process exec another program. In Python, we fork a child process through the subprocess package in the standard library and run an external program (Fork,exec see Linux process Basics).

The subprocess package defines a number of functions that create child processes, each of which creates child processes in different ways, so we can choose one to use as needed. In addition, Subprocess provides some tools for managing standard streams and pipelines (pipe) to use text communication between processes.

When creating child processes using the functions in the subprocess package, be aware that:

1) Whether the parent process pauses after the child process is created and waits for the child process to run.

2) What the function returns

3) How the parent process handles when ReturnCode is not 0 o'clock.

Subprocess.call ()



Parent process waits for child process to complete

Return exit information (ReturnCode, equivalent to exit code, see Linux process Basics)

Subprocess.check_call ()


Parent process waits for child process to complete

Returns 0

Check the exit information, and if ReturnCode is not 0, cite error subprocess. Calledprocesserror, this object contains the ReturnCode property, which can be try...except ... To check (see Python error handling).

Subprocess.check_output ()



Parent process waits for child process to complete

Returns the output of the child process to the standard output

Check the exit information, and if ReturnCode is not 0, cite error subprocess. Calledprocesserror, which contains the ReturnCode property and the output property, outputs a result of the standard output, available try...except ... To check.

These three functions are used in a similar way, and we use Subprocess.call () to illustrate:

Import subprocessrc = Subprocess.call (["LS", "-l"])



We put the program name (LS) together with the parameter (-L) in a table passed to Subprocess.call ()

A shell can be used to interpret an entire string:

Import subprocessout = Subprocess.call ("Ls-l", shell=true) out = Subprocess.call ("CD:", Shell=true)



We used the Shell=true parameter. At this point, we use an entire string instead of a table to run the child process. Python will run a shell first and then use this shell to interpret the entire string.

Some of the shell commands are built-in shell commands that must be run through the shell, $CD. Shell=true allows us to run some of these commands.

Popen ()

In fact, the three functions above are based on the Popen () package (wrapper). The purpose of these packages is to make it easy for us to use child processes. When we want to personalize our needs more, we turn to the Popen class, which produces objects that represent child processes.

Unlike the package above, the main program does not automatically wait for the child process to complete after the Popen object is created. We must call the object's Wait () method before the parent process waits (that is, block block):

Import Subprocesschild = subprocess. Popen (["Ping", "-C", "5", "www.google.com"]) print ("Parent process")



As seen from the results of the run, the parent process does not wait for the child to complete after it has been opened, but runs print directly.

Comparison of waiting conditions:

Import Subprocesschild = subprocess. Popen (["Ping", "-C", "5", "www.google.com"]) child.wait () print ("Parent process")



In addition, you can also perform other operations on the child process in the parent process, such as in our example:

Child.poll ()      # Check child process status Child.kill ()      # terminating child process child.send_signal ()  # Send signal to Child process child.terminate ()   # terminating child process


The PID of the child process is stored in the Child.pid

Text flow control for child processes

Standard input, standard output, and standard errors can also be represented by the following properties (in the child process):

Child.stdin

Child.stdout

Child.stderr

We can change standard input, standard output, and standard error when Popen () is established, and can take advantage of subprocess. Pipes connect the inputs and outputs of multiple sub-processes together to form a pipeline (pipe):

Import subprocesschild1 = subprocess. Popen (["LS", "-l"], stdout=subprocess. PIPE) child2 = subprocess. Popen (["WC"], stdin=child1.stdout,stdout=subprocess. PIPE) out = Child2.communicate () print (out)


Subprocess. Pipe actually provides a buffer for text flow. The child1 stdout the text out to the buffer, and then Child2 's stdin reads the text from the pipe. The output text of the child2 is also stored in the pipe until the communicate () method reads the text from the pipe.

Note that communicate () is a method of the Popen object that blocks the parent process until the child process finishes.

We can also use the communicate () method to input a child process using a pipe:

Import Subprocesschild = subprocess. Popen (["Cat"], stdin=subprocess. PIPE) child.communicate ("Vamei")


After we start the subprocess, cat waits for input until we enter "Vamei" with communicate ().

By using the subprocess package, we can run external programs. This greatly expands the functionality of Python. If you already know some of the operating system's applications, you can call the app directly from Python (rather than relying entirely on Python), and output the app's results to Python and let Python continue processing. Shell functions, such as using text streams to connect to individual applications, can be implemented in Python.

Summarize

Subprocess.call, Subprocess.check_call (), Subprocess.check_output ()

Subprocess. Popen (), subprocess. PIPE

Popen.wait (), Popen.communicate ()


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.