The use of the Python third Party library series 22--subprocess

Source: Internet
Author: User
Tags stdin
One, why to Subprocess
Starting with Python 2.4, Python introduced the subprocess module to manage the subprocess to replace the methods of some old modules: Os.system, os.spawn*, os.popen*, popen2.*, commands.*, Not only can the external command be invoked as a child process, but it can be connected to the Input/output/error pipeline of the child process to obtain related return information.

Second, 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.

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

(2) 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

(3) 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 ():
>>> 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:
>>> 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.

(4) subprocess. Popen
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:
>>> 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:
>>> 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:
Child.poll ()           # Check child process status
Child.kill ()           # terminating subprocess
child.send_signal ()    # Sending signals
to child processes Child.terminate ()      # The
pid stored in the child process subprocess Child.pid

(5) The standard input, standard output, and standard error of the text flow control subprocess of the subprocess are 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:
>>> 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

Third, plus timeout processing
def system_exec (args, timeout=2, Shell=false, interval=0.01): ' @args: command array ' retval = 0 Output = "Success" End_time = Datetime.datetime.now () + Datetime.timedelta (seconds=timeout) Try: # OUTP UT = subprocess.check_output (args, Shell=shell) # Pipelines that do not specify standard output and error output are printed to the screen; sub = subprocess. Popen (args, stdout=subprocess.
        PIPE, Shell=shell, bufsize=4096) #subprocess. Poll () method: Check that the subprocess is over, and if it ends, set and return the code and place it in the Subprocess.returncode variable
                While Sub.poll () is None:time.sleep (interval) if End_time <= Datetime.datetime.now ():

        Sub.terminate () return ( -1, "Command Timeout") else:pass # # Read all lines from stdout with sub.stdout as Fd:output = Fd.read () # Read () or readlines () retval = sub.returncode output = Output.strip () # # Strip The last \ n fd.close () except E XceptIon as E:retval =-1 output = str (e) return retval, output 


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.