subprocess Module
0 Module Description /Module Description
From subprocess module:
"""Subprocesses with accessible I/O streams this module allows the spawn processes, connect to their Input/output/error p Ipes, and obtain their return codes. For a all description of this module see the Python documentation. Main API ======== Run (...): Runs a command, waits for it, and then returns a Completedprocess instance. Popen (...): A class for flexibly executing a command in a new process Constants---------devnull:special value that ind Icates that Os.devnull should is used pipe:special value that indicates a PIPE should be created Stdout:special Valu E that indicates this stderr should go to stdout older API ========= call (...): Runs a command, waits for it to complete , then returns the return code. Check_call (...): Same as Call () but raises calledprocesserror () if return code was not 0 check_output (...): Same as Che Ck_call () but returns the contents of StdOut instead of a return code getoutput (...): Runs a command in the shell, WAIt for it to complete, then returns the output Getstatusoutput (...): Runs a command in the shell, waits for it to COM Plete, then returns a (status, output) tuple"""
1 Constants /Constants
1.0 PIPE Constants
constant Value : PIPE =-1
constant Function : A special value that indicates the need to create a pipe. Passing this variable to STDOUT/STDIN/STDERR enables the output of the child process to be passed to the parent process
1.1 STDOUT Constants
constant Value : STDOUT = 2
constant Function : A special value indicating that the stderr needs to be transferred to stdout
1.2 Devnull Constants
constant Value : devnull = 3
constant Function : A special value indicating the need to use the Os.devnull
2 function /Function
2.0 run () function
function Call : Re = Subprocess.run (*popenargs, Input=none, Timeout=none, Check=false)
function function : Create a new process to run the program and return the Completedprocess instance of the new process
Incoming Parameters : *popenargs, input, timeout, check
*popenargs:list type, input used when invoking a new process
Input:obj type, used to set the input of a new process
Timeout:int type, sets the time-out limit and causes timeoutexpired if the process takes too long
Check:bool type, detection program exit, if the exit code is not 0, trigger Calledproecsserror
return Parameters : Re
Re:instance type, returned completedprocess instance
2.1 call () function
function Call : Re = Subprocess.call (*popenargs, Stdin=none, Stdout=none, Stderr=none, shell=false, Timeout=none)
function function : Create new process run program, input output bind to parent process, return new process exit code
Incoming Parameters : *popenargs, stdin, stdout, stderr, Shell, timeout
*popenargs:list type, input used when invoking a new process
Stdin:obj type, used to set the input of a new process
Stdout:obj type, used to set the output of a new process
Stderr:obj type, which sets the error message for the new process
Shell:bool type, set whether to use intermediate shell (can use shell-related variables, etc.)
Timeout:int type, setting time-out limit
return Parameters : Re
Re:int type, return exit code, 0 for normal exit
Note: for the input parameters of the new process, incoming in list form, such as command Python test.py, pass in the parameter list [' Python ', ' test.py '].
2.2 Check_call () function
function call : Re = Subprocess.check_call (*popenargs, Stdin=none, Stdout=none, Stderr=none, Shell=false, Timeout=none)
function function : Create a new process to run the program, the input output bound to the parent process, normal exit return exit code 0, or throw a subprocess. Calledprocesserror
Incoming Parameters : *popenargs, stdin, stdout, stderr, Shell, timeout
*popenargs:list type, input used when invoking a new process
Stdin:obj type, used to set the input of a new process
Stdout:obj type, used to set the output of a new process
Stderr:obj type, which sets the error message for the new process
Shell:bool type, set whether to use intermediate shell (can use shell-related variables, etc.)
Timeout:int type, setting time-out limit
return Parameters : Re
Re:int type, return exit code, 0 for normal exit
2.3 getstatusoutput () function
function Call : Re = subprocess.getstatusoutput (cmd)
function function : Create new process Run program, return new process exit code and output in tuple form
Incoming Parameters : cmd
Cmd:list type, input used when invoking a new process, [' Python ', ' test.py '] form
return Parameters : Re
Re:tuple type, returned tuple, containing exit code and output
2.4 getoutput () function
function Call : Re = subprocess.getoutput (cmd)
function function : Create a new process to run the program, returning the output of the new process as a string
Incoming Parameters : cmd
Cmd:list type, input used when invoking a new process, [' Python ', ' test.py '] form
return Parameters : Re
RE:STR type, return child process output
2.5 check_output () function
function Call : Re = subprocess.check_output (*popenargs, Input=none, Stdin=none,
Stdout=none, Stderr=none, Shell=false, Universal_newlines=false, Timeout=none)
function function : creates a new process to run the program, returning the output of the new process
Incoming Parameters : *popenargs, input, stdin, stdout, stderr, Shell, Universal_newlines, timeout
*popenargs:list type, input used when invoking a new process
INPUT:BYTE/STR type, an additional available input that allows the passing of a word (b) string to stdin
Stdin:obj type, used to set the input of a new process
Stdout:obj type, used to set the output of a new process
Stderr:obj type, which sets the error message for the new process
Shell:bool type, set whether to use intermediate shell (can use shell-related variables, etc.)
Universal_newlines:bool type, set input/output format, false to Byte,true as Str
Timeout:int type, setting time-out limit
return Parameters : Re
RE:BYTE/STR type, returned output result
3 class /Class
3.1 Popen class
class Instantiation: PRCs = subprocess. Popen(args, Stdin=none, Stdout=none, Stderr=none, [...])
functions of the class : used to generate a new process execution subroutine
Incoming Parameters : Args
Args:list type, input for new process execution
Stdin:obj/int type, used to set the input of a new process
Stdout:obj/int type, used to set the output of a new process
Stderr:obj/int type, which sets the error message for the new process
return Parameters : PRCs
Prcs:instance type, generated new process instance
Note: for the input parameters of the new process, args, passed in as list, such as command Python test.py, pass in the parameter list [' Python ', ' test.py '].
3.1.1 PID Properties
Property Call : pid = prcs.pid
Properties Feature : Returns the PID information for a child process
Property Parameters : PID
Pid:int type, PID of child process
3.1.2 Communicate () Method
function Call : Re = prcs.communicate (Input=none, Timeout=none)
function function : used for communication between processes, sending data to stdin, and reading data from stdout and stderr
Incoming Parameters : input, timeout
INPUT:BYTE/STR type, an additional available input that allows the passing of a word (b) string to stdin
Timeout:int type, setting time-out limit
return Parameters : Re
Re:tuple type, returned output, (stdout, stderr)
3.1.3 Poll () Method
function Call : Re = Prcs.poll ()
function function : used to detect whether a child process has ended
Incoming Parameters : No
return Parameters : Re
Re:int type, returns the result that is 1 the child process has ended
3.2 completedprocess class
class Instantiation: Re = Subprocess.run ()/completedprocess (args, ReturnCode, Stdout=none, Stderr=none)
functions of the class : A child process that has finished running, typically returning a build when calling the Subprocess.run () function
Incoming Parameters : args, ReturnCode, stdout, stderr
Args:list type, input for new process execution
Returncode:int type, the child process exit code
Stdout:obj/nonetype type, output of child process, none if not obtained
Stderr:obj/nonetype type, error message for child process, none if not obtained
return Parameters : Re
Re:instance type, generated end child process instance
Python concurrent parallel [3]--process--subprocess module