一 功能:
*spawn new processes,connect to their input/output/error pipes, and obtain their return codes.
*intends to replace several older modules and functions:os.system,os.popen 等等.
二 subprocess模块属性:
(1)call(args, *, stdin=None, stdout=None, stderr=None, shell=False)
*Run the command described by args. Wait for command to complete, then return the returncode attribute.
*Do not use stdout=PIPE or stderr=PIPE with this function as that can deadlock based on the child process output volume. Use Popen with the communicate() method when you need pipes.
(2)check_all(args, *, stdin=None, stdout=None, stderr=None, shell=False)
*Run command with arguments. Wait for command to complete. If the return code was zero then return, otherwise raise CalledProcessError. The CalledProcessError object will have the return code in the returncode attribute.
(3)check_output(args, *, stdin=None, stderr=None, shell=False, universal_newlines=False)
*Run command with arguments and return its output as a byte string.
*If the return code was non-zero it raises a CalledProcessError. The CalledProcessError object will have the return code in the returncode attribute and any output in the output attribute.
(4)PIPE:
*Special value that can be used as the stdin, stdout or stderr argument to Popen and indicates that a pipe to the standard stream should be opened.
(5)STDOUT
*Special value that can be used as the stderr argument to Popen and indicates that standard error should go into the same handle as standard output.
(6)exception subprocess.CalledProcessError
*Exception raised when a process run by check_call() or check_output() returns a non-zero exit status.
(7)Popen:下节介绍。
三 Popen类方法:
(1)Popen.poll():
*Check if child process has terminated. Set and return returncode attribute.
(2)Popen.wait():
*Wait for child process to terminate. Set and return returncode attribute.
(3)Popen.communicate(input=None):
*Interact with process: Send data to stdin. Read data from stdout and stderr, until end-of-file is reached. Wait for process to terminate. The optional input argument should be a string to be sent to the child process, or None, if no data should be sent to the child.
*communicate() returns a tuple (stdoutdata, stderrdata).
(4)Popen.send_signal(signal):
*Sends the signal signal to the child.
(5)Popen.terminate():
*Stop the child. On Posix OSs the method sends SIGTERM to the child. On Windows the Win32 API function TerminateProcess() is called to stop the child.
(6)Popen.kill():
*Kills the child. On Posix OSs the function sends SIGKILL to the child. On Windows kill() is an alias for terminate().
(7)Popen.returncode
*The child return code, set by poll() and wait() (and indirectly by communicate()). A None value indicates that the process hasn’t terminated yet.