The main function of the ubprocess package is to execute external commands and programs. 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.
In Python, we fork a child process through the subprocess package in the standard library and run an external program.
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)
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,
The object contains the ReturnCode property, which can be try...except ... To check.
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,
The object contains the ReturnCode property and the output property, which outputs a standard output, which can be try...except ... To check.
These three functions are used in a similar way, with Subprocess.call () to illustrate:
Import subprocess
rc = Subprocess.call (["LS", "-l"])
Place the program name (LS) with the parameter (-L) in a table and pass it to Subprocess.call ()
A shell can be used to interpret an entire string:
Import Subprocessimport subprocesschild = subprocess. Popen (["Ping", "-C", "5", "www.google.com"]) print ("Parent process")
out = Subprocess.call ("Ls-l", shell=true) out = Subprocess.call ("CD..", Shell=true)
Using the Shell=true parameter, this time, 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):
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 a signal to a 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
The standard input for child processes, standard output, and standard errors can also be represented by the following properties:
Child.stdin
Child.stdout
Child.stderr
We can change standard input, standard output, and standard error when popen () establishes a subprocess.
and can use 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.
Python Learning Note 13: Child process of the standard library (subprocess package)