Vamei Source: Http://www.cnblogs.com/vamei Welcome reprint, Please also keep this statement. Thank you!
Reference links
Http://www.cnblogs.com/vamei/archive/2012/09/23/2698014.html
The concept of a parent-child process
A process can fork itself, become a child process, and let the child process to exec another program
Sub-processes in Python
In Python, with the subprocess package, fork a child process and run an external program
Points to think about
Whether the parent process blocks after the child process is created
What the function returns
ReturnCode not 0 o'clock (description is not a child process), how the parent process handles
Simple to use Subprocess.call ()
Parent process waits for child process to complete
Return exit information (ReturnCode)
Subprocess.check_call ()
Parent process waits for child process to complete
Returns 0
The exit information (ReturnCode) is checked and subprocess is thrown if ReturnCode is not 0. Calledprocesserror error, the object contains a ReturnCode property, available 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
The exit information (ReturnCode) is checked and subprocess is thrown if ReturnCode is not 0. The Calledprocesserror error, which contains the ReturnCode property and the output property, outputs a standard output, and is also available try...except ... To check
Simple Use example list parameter
The program name ls and parameter-L are placed in a table passed to Subprocess.call ()
rc = Subprocess.call (["LS", "-l"])
String argument
Run a shell first, then use the shell to interpret the entire string
Some of the shell's built-in commands must be run using the shell
out = Subprocess.call ("Ls-l", Shell=true)
Advanced usage-Child process parallelism & text Flow subprocess. Popen class
__init__ (self, 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)
After the Popen object is created, the parent process does not automatically wait for the child process to complete
Child = subprocess. Popen (["Ping", "-C", "5", "www.google.com"])
Child.pid-PID of child process
Child.stdin-stdin of child processes
Child.stdout-stdout of child processes
Child.stderr-stderr of child processes
Child.wait ()-Parent process waits for child process to complete
Child.poll ()-Check child process status
Child.send_signal (SIG)-sends a signal to a child process
Child.kill ()-Terminates child process
Child.terminate ()-Terminates the child process, equal to kill
Child.communicate (Input=none)-After giving the child process stdin, gets the output stream of the child process, returning the result to the tuple type (STDOUT,STDERR). Be aware that this method will block the current process.
Advanced use example Parent-child process parallelism
Child = subprocess. Popen (["Ping", "-C", "5", "www.google.com"])
Print ("Parent process")
Child process stdout directs another subprocess stdin
Child1 = subprocess. Popen (["LS", "-l"], stdout=subprocess. PIPE)
Child2 = subprocess. Popen (["WC"], stdin=child1.stdout,stdout=subprocess. PIPE)
out = Child2.communicate ()
The child process waits for the current process input
Child = subprocess. Popen (["Cat"], stdin=subprocess. PIPE)
Child.communicate ("Vamei")
Python parallel operation-parent-child process Subprocess Library