Python subprocess Module Learning Summary _python

Source: Internet
Author: User

First, subprocess and the commonly used encapsulation functions
When we run Python, we're both creating and running a process. Like the Linux process, a process can fork a subprocess 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.
The subprocess package defines a number of functions that create child processes, which create child processes in different ways, so we can select one to use as needed. In addition, Subprocess provides tools for managing standard streams (standard stream) and pipelines (pipe) to use text communication between processes.

Subprocess.call ()
The parent process waits for the child process to complete
Returns the exit information (ReturnCode, equivalent to Linux exit code)

Subprocess.check_call ()
The parent process waits for the child process to complete
return 0
Check the exit information, and if the ReturnCode is not 0, cite the error subprocess. Calledprocesserror, this object contains ReturnCode properties, available try...except ... To check

Subprocess.check_output ()
The parent process waits for the child process to complete
Returns the output of the child process to the standard output
Check the exit information, and if the ReturnCode is not 0, cite the error subprocess. Calledprocesserror, the object contains ReturnCode and output properties, output properties for standard output, available try...except ... To check.

These three functions are used in similar ways, and the following examples are illustrated with Subprocess.call ():

Copy Code code as follows:

>>> Import subprocess
>>> retcode = Subprocess.call (["LS", "-l"])
#和shell中命令ls-A shows the same result
>>> Print Retcode
0

The program name (LS) is passed to Subprocess.call () along with the parameter (-L) in a table.

Shell defaults to False, under Linux, when Shell=false, Popen calls OS.EXECVP () to execute args specified program; shell=true, if Args is a string, Popen directly calls the system's shell to perform args-specified programs, if args is a sequence, the first item of args is to define the program command string, and other items are additional parameters when calling the system shell.

The above examples can also be written as follows:

Copy Code code as follows:
>>> Retcode = Subprocess.call ("Ls-l", Shell=true)

Under Windows, Popen calls CreateProcess () to execute the external program specified by args, regardless of the shell's value. If args is a sequence, convert the List2cmdline () to a string, but note that not all programs in MS Windows can be converted to command line strings using List2cmdline.

Subprocess. Popen ()

Copy Code code as follows:

Class Popen (args, bufsize=0, Executable=none, Stdin=none, Stdout=none, Stderr=none, Preexec_fn=none, Close_fds=false, Shell=false, Cwd=none, Env=none, Universal_newlines=false, Startupinfo=none, creationflags=0)

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

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

Copy Code code as follows:
>>> Import subprocess
>>> child = subprocess. Popen ([' Ping ', '-C ', ' 4 ', ' blog.linuxeye.com '])
>>> print ' parent process '

As you can see from the run results, the parent process does not wait for the child to complete but instead runs print directly after the subprocess is opened.

Compare the waiting situation:

Copy Code code as follows:
>>> Import subprocess
>>> child = subprocess. Popen (' Ping-c4 blog.linuxeye.com ', shell=true)
>>> child.wait ()
>>> print ' parent process '

As seen from the run result, the parent process runs print after the subprocess is opened and waits for the child to complete.
In addition, you can do other things in the parent process, such as the child object in our example above:
Copy Code code as follows:

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

PID for child processes is stored in child.pid
text flow control for sub-processes
Standard input, standard output, and standard errors for child processes are represented by the following properties:
Copy Code code as follows:

Child.stdin
Child.stdout
Child.stderr

You can change standard input, standard output, and standard errors when Popen () establishes a subprocess, and you can use subprocess. Pipe joins the inputs and outputs of multiple child processes together to form a pipeline (pipe), as follows 2 examples:
Copy Code code as follows:
>>> Import subprocess
>>> child1 = subprocess. Popen (["LS", "-l"], stdout=subprocess. PIPE)
>>> print Child1.stdout.read (),
#或者child1. Communicate ()
>>> Import subprocess
>>> child1 = subprocess. Popen (["Cat", "/etc/passwd"], stdout=subprocess. PIPE)
>>> child2 = subprocess. Popen (["grep", "0:0"],stdin=child1.stdout, stdout=subprocess.) PIPE)
>>> out = Child2.communicate ()

Subprocess. Pipe actually provides a buffer for the text stream. Child1 's stdout outputs the text to the buffer, and then child2 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 in pipe from the pipe.
Note: Communicate () is a method of the Popen object that blocks the parent process until the child process completes

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.