Directory
Directory preface software Environment Knowledge Subprocess Popen Constructor constructor Class Popen parameter args calling program call shell instruction Stdinstdoutstderr real-time fetch subroutine output one time get all output of subroutine Output the standard error and standard output together with the shell bufsize Close_fds other parameter meaning Popen member function Popenpoll popenwaittimeoutnone Popencommunicateinputnonetimeoutnone popensend_signalsignal popenterminate popenkill popen member Properties Popenpid Popenreturncode Popenstdin Input Interaction popenstdout Continuous input-Output interaction Popenstderr subprocess function Subprocesscall Subprocesscheck_output Last
Preface
Subpocess is used to create child processes in the parent process, if you want to invoke external programs in a Python program, such as: Powershell, Shell, cmd, bat. Subprocess will be a very good choice. Software Environment system
Win 10 Software
Python 3.4.4 IPython 4.0.0 knowledge subprocess
Or that sentence, the most efficient way to look at the official document, Portal: here
subprocess: The subprocess module allows to spawn new processes, connect to their input/output/error, a D obtain their return codes. This module intends to replace several older modules and functions:
Os.system
os.spawn*
os.popen*
popen2.*
commands.*
The birth of subprocess is to replace, the integration of several previous methods of creating child processes, to achieve the form of pipelines to connect the subprocess of stdin,stdout,stderr, And the child process returns a ReturnCode to the parent process. Similar functions are implemented with Fock in C language.
Execute a child program in a new process. On POSIX, the class uses OS.EXECVP () is like behavior to execute the child program. On Windows, the class uses the Windows CreateProcess () function
The OS.EXECVP () is used in the POSIX type system to execute the subroutine, and the CreateProcess () function is used in the Windows environment, which is primarily recorded for use in Windows environments. Popen Constructor (constructor)
Subprocess has several functions that create child processes in different ways, but Subprocess has only one Popen class, which is also used to create child processes. Objects created using class Popen have Popen member properties and methods.
Class subprocess. Popen (
args,
bufsize=-1,
executable=none,
stdin=none,
stdout=none, Stderr=none
, Preexec_fn=none,
close_fds=true,
shell=false,
cwd=none,
env=none,
universal_newlines= False,
Startupinfo=none,
creationflags=0,
restore_signals=true,
Start_new_session=false,
pass_fds= ())
The version Popen object that follows Python 3.2 adds the following syntax:
on exit, standard file descriptors are closed, and the process was waited for.
With Popen (["Ifconfig"], Stdout=pipe) as proc:
Log.write (Proc.stdout.read ())
The parameter implications of the Popen class are described below. parameters for Class popen args
args : should be a sequence of the program arguments or else a single string.
The args parameter can be either a string type or a sequence type, as a declaration of a subroutine. The child process APIs invoked in Windows are CreateProcess ([String]), so strings such as Notepad.exe Test.txt can be executed. However, in the Linux environment, you need to accept the list type object to delimit the program name and parameters. In the case of a sequence type, the first parameter of the sequence is generally the path to the subroutine, followed by the parameters of the incoming subroutine. The official suggested that the args parameter use the list type Object . Calling Programs
invoke a PowerShell script:
args = [r "C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe", "-executionpolicy", "unrestricted", R "E:\" Users\oe-fanguiju\desktop\sendmail.ps1 ", str (BODYSTR)]
PS = subprocess. Popen (args,stdout=subprocess. PIPE)
Note : Add R before string to avoid SyntaxError: (Unicode error) calling shell directives
serialization of incoming parameters Shlex.split ():
We can also use the Shlex.split () function to serialize the instructions we need to execute and then assign the values to the args parameters.
>>> import Shlex, subprocess
>>> command_line = input ()
/bin/vikings-input eggs.txt-output " Spam Spam.txt "-cmd" Echo ' $MONEY ' "
>>> args = Shlex.split (command_line)
>>> print (args)
['/bin/vikings ', '-input ', ' eggs.txt ', '-output ', ' spam spam.txt ', '-cmd ', ' echo ' $MONEY ']
>>> p = Sub Process. Popen (args) # success!
Stdin\stdout\stderr
STDIN\STDOUT\STDERR specifies the standard input, output, and incorrect file handle for the subroutine (file handles). They can be pipe pipes, devnull (devnull indicates that special file Os.devnull.), file descriptor (used files existing a positive integer Or a file object that already exists. Redirection does not occur when his value is none, and the file handle of the child process inherits the parent process. and stderr=subprocess. STDOUT can set standard error file handle to standard output (standard error of subroutine convergence to standard output). Specifies the Stdout/stderr as subprocess. PIPE, so that when the popen is invoked, a pipe is established between the parent process and the child process, and the standard output and error output of the child process are redirected to the pipeline, which can be acquired by the parent process. get subroutine output in real time
p = subprocess. Popen ("/etc/service/tops-cmos/module/hadoop/test.sh", Shell=true, stdout=subprocess. PIPE, Stderr=subprocess. STDOUT)
ReturnCode = P.poll () while
ReturnCode are None: #检查子程序是否结束 Line
= P.stdout.readline () # If not, get the output of the subroutine
ReturnCode = P.poll () line
= Line.strip ()
print line
print ReturnCode
This allows the output of the child process to be obtained in real time. get all the output of a subroutine at a time
When you want to get all the child process outputs at once after the subroutine has been executed, the child process object can invoke communicate (), and he will block until the child process is finished, waiting for the output returned by the child process.
PS = subprocess. Popen (args,stdout=subprocess. PIPE)
Psallreturn = Ps.communicate ()
#Or: Psreturn = Ps.stdout.read () return
Psreturn
The execution result is returned only after the execution of the subroutine has finished.
Note : Communicate () closes the pipe after one communication. This method is not recommended if you want to communicate frequently between processes.
You can try the following methods:
P= subprocess. Popen (["WC"], stdin=subprocess. Pipe,stdout=subprocess. Pipe,shell=true)
p.stdin.write (' Your command ') #传递数据给子进程
p.stdin.flush () #清空子进程管道缓存
# ... Do something
try:
#......do something
p.stdout.readline () #获取子进程输出
#......do something
Except:
print (' IOError ')
#......do something more
p.stdin.write (' Your other command ')
P.stdin.flush ()
output standard error and standard output together
PS = subprocess. Popen (args,stdout=subprocess. Pipe,stderr=subprocess. STDOUT)
This way, both standard output and standard error output are returned to the parent process through the STDOUT pipeline. Shell
The shell argument (which defaults to False) specifies whether to use the shell as the "program" to execute. The If shell is True, the it is recommended to pass args as a string rather than as a sequence.
The shell specifies whether to use a shell program to execute the subprocess, and when shell = True , the subprocess is executed by the shell, and it is recommended that the args parameter use a String object.
If args is a string, the string specifies the command to execute through the shell.
in [[]: P = subprocess. Popen ("Ipconfig/all", stdout = subprocess. pipe,shell=true) #args = "Ipconfig/all" in
[m]: PRet = P.stdout.read () #父进程获得执行结果
#On Windows with Shell=true, the COMSPEC environment variable specifies the default shell.
#Win下相当于 ' args = ["cmd.exe", "ipconfig", "/all"]
On POSIX with shell=true, the shell defaults to/bin/sh.
Linux is equivalent to args = ["/bin/sh", "C", Args[0], args[1], ...] bufsize
BufSize'll be supplied as the corresponding argument to the ' open ' () function when creating the Stdin/stdout/stderr pip E file objects.
0 means unbuffered (read and write are one system call and can return short)
1 means line buffered (only usable if universal_newlines=true i.e., in a text mode)
no other positive value means use a buffer of approximately that size
negative bufsize (the default) means the Syste M default of Io. Default_buffer_size would be used.
When you redirect stdin/stdout/stderr to a pipe or file object, BufSize can pass a specified buffer to the open () function, creating a file object:
0 means no buffering;
1 indicates line buffering;
Othernumber represents the buffer size, and
-1 is the default value of the BufSize parameter to use system buffering (full buffering)
Note : When the child process returns data that reaches the cached size, The subroutine waits for the payment process to read the cached data. Close_fds
If Close_fds is true, all file descriptors except 0, 1 and 2 'll be closed before the ' child ' process is executed. (POSIX only). The default varies by platform:always true on POSIX. On Windows it are true when Stdin/stdout/stderr are None, false otherwise. On Windows, if Close_fds is true then no handles would be inherited by the child process. Note this on Windows, with cannot set Close_fds to True and also redirect the standard handles by setting stdin, stdout or StdErr.
In Unix, if Close_fds = True, file descriptors other than 0, 1, 2 will be closed. If Stdin/stdout/stderr = None in Windows, that is, when the parent process is inherited, Close_fds = True, and false instead. Other file descriptors are not inherited under Windows.
Note : In Windows, you cannot redirect (redirect) The stdin, stdout or stderr on the premise of Close_fds = True, and lock the stdin/stdout/stderr of the child process.
in [[]: P = subprocess. Popen ("ipconfig", shell=true,close_fds=true,stdout=subprocess. PIPE)
Valueerror:close_fds is isn't supported on Windows platforms if you redirect Stdin/stdout/stderr
Other parameter Meanings
given, Startupinfo: If Given, startupinfo'll be a Startupinfo object, which was passed to the underlying CREATEPR ocess function. Creationflags, if given, can be create_new_console or create_new_process_group. (Windows only)
In Windows, when the CreateProcess () function is invoked to create a subprocess, the Startupinfo parameter passes the Startupinfo object to set the appearance of the child process, and so on.
executable: Specifies the name of the program to execute, rarely used, and generally uses the args parameter to specify the subroutine that needs to be executed. You can specify the shell (e.g Bash, csh, zsh) that executes the child process. Under UNIX, the default is/bin/sh. Windows, is the value of the environment variable%comspec%. Under Windows, you only need to specify shell=true when the command you want to execute is really a shell-built command (such as dir, copy)
, and you do not need to specify this when you want to execute a batch script based on the command line.
C:\users\username>echo%comspec%
C:\WINDOWS\system32\cmd.exe
preexec_fn: If PREEXEC_FN is set to a callable object, this object would be called in the child process just before The child is executed. (POSIX only)
The hook function, which is only valid on the UNIX platform, is used to specify an executable object (callable object) that will be invoked before the child process runs. Easy to cause deadlock, use caution.
cmd: Specifies the working directory of the child process
Note : This directory will not be the search directory for executable files, so do not set the directory where the program files are located as CWD.
env: is a dictionary type that executes the execution variables of a child process, rather than using the environment variables that inherit the parent process by default
universal_newlines: True, the stdout and stderr of the subprocess are treated as text objects, whether it is the line terminator '/n ' of Unix, the line terminator '/R ' in MAC format, or the line terminator in Windows format '/r/n ' will be treated as '/n '.
Pass_fds: is a optional sequence of file descriptors to keep open between the parent and child. Providing any Pass_fds forces Close_fds to is True. (POSIX only)
restore_signals: If Restore_signals is True (the default) all signals this Python has set to Sig_ign are To SIG_DFL in the child process before the exec. Currently this includes the Sigpipe, SIGXFZ and SIGXFSZ signals. (POSIX only)
start_new_session: If start_new_session is True the Setsid () system call'll be made in the child process prior t o The execution of the subprocess. (POSIX only) Popen member functions Popen.poll ()
Check If child process has terminated. Set and return ReturnCode attribute.
Used to check whether the child process has ended. Sets and returns the ReturnCode property popen.wait (Timeout=none)
Wait for the child process to terminate. Set and return ReturnCode attribute.
Wait for child process to end. Sets and returns the ReturnCode property. When you create a subprocess using the Popen class, the parent process does not wait for the child process to end by default, and requires the call-wait () function to implement the waiting child process to finish the Hofu process.
Timeout: If The process does not terminate after timeout seconds, raise a timeoutexpired exception. It is safe to catch this exception and retry the wait.
Note: If the child process outputs a large amount of data to a stdout or stderr pipeline and reaches the system pipe cache size, the subprocess waits for the parent process to read the data in the pipe, and if the parent process is waiting (), the data in the pipe will not be read, causing the deadlock, It is recommended that you use Pepon.communicate () to avoid this situation. popen.communicate (Input=none,timeout=none)
Interacts with a child process. Sends data to STDIN, optionally parameter input specifies the data to be sent to the child process.
Communicate () reads data from stdout and stderr until EOF at the end of the text (text communication between processes) waits for the child process to end. Returns a tuple: (Stdout_data, Stderr_data), the data would bebytes or, if Universal_newlines is True, strings. Returns a string if Universal_newlines = True.
Note : If you want to send data to a child process through stdin, you need to create a pipe object through Stdin=pipe. Similarly, if you want to get data from stdout and stderr, you must set the STDOUT and stderr to pipe.
P=subprocess. Popen (cmd, Shell=true, stdout=subprocess.) PIPE, Stderr=subprocess. STDOUT)
(stdout_data, stderr_data) = P.communicate ()
Note: If The process does not terminate after timeout seconds, a timeoutexpired exception would be raised. Catching this exception and retrying communication would not lose any output.
If timeout has not yet finished the process, it is necessary to catch the triggered timeoutexpired exception and use Popen.communicate () to preserve the output of the subroutine.
proc = subprocess. Popen (...)
Try:
outs, errs = Proc.communicate (timeout=15)
except timeoutexpired: Proc.kill
() outs
, errs = Proc.communicate ()
popen.send_signal (signal)
Sends a signal to a child process. popen.terminate ()
Stops the child process. Under Windows platform, this method calls the Windows API terminateprocess () to end the subprocess. Popen.kill ()
Kill the child process and use the terminateprocess () API in Windows. The equivalent of sending signal sigterm and Sigkill on UNIX. Popen member Properties Popen.pid
Gets the process ID of the child process. Popen.returncode
Gets the return value of the process. Returns none if the process is not yet finished. Popen.stdin
If the stdin argument was PIPE, this attribute is a writeable stream object as returned by open () input interaction
#test. py
Import sys line
= sys.stdin.readline ()
print ' Test ', line
#run. py from
subprocess Import *
p = Popen ('./test.py ', stdin=pipe,stdout=pipe)
p.stdin.write (' Say hi/n ')
print p.stdout.readline ()
Popen.stdout
If The stdout argument was PIPE, this attribute is a readable stream object as returned by open (). continuous input-Output interaction
# test.py
Import sys while
True: line
= Sys.stdin.readline ()
if not line:break
sys.stdout.write ( Line)
Sys.stdout.flush ()
# run.py
the import sys
from subprocess import *
proc = Popen ('./test.py ', Stdin=pipe,stdout=pipe,shell=true in
Sys.stdin:
proc.stdin.write (line) #子进程的输入
Proc.stdin.flush ()
output = Proc.stdout.readline () #子进程的输出
sys.stdout.write (output) #父进程的打印
Note : run.py flush and test.py flush, remember to empty the buffer, otherwise the program does not get the correct input and output Popen.stderr
If the stderr argument was PIPE, this attribute is a readable stream object as returned by open (). subprocess Function
The subprocess function is essentially the subprocess. Popen encapsulation, it is easy to create a child process. When you need to create more complex child processes, it is recommended that you use the Popen class, which generates child process objects and has a variety of member methods and properties. Subprocess.call ()
Subprocess.call (args, *, Stdin=none, Stdout=none, Stderr=none, Shell=false, Timeout=none)
The parent process waits for the child process to complete
Returns the exit information (ReturnCode, equivalent to Linux exit code)
In [m]: Subprocess.call ("ipconfig")
.
OUT[20]: 0
Subprocess.check_call ()
Subprocess.check_call (args, *, Stdin=none, Stdout=none, Stderr=none, Shell=false, Timeout=none)
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
in [[]: Subprocess.check_call ("config", shell=true)
calledprocesserror:command ' config ' returned Non-zero exit Status 1
the difference between call () and Check_call () : The difference is that when the error is encountered, call () returns ReturnCode, Check_call () returns ReturnCode and throws an exception. Check_call actually invokes the call function and then joins the exception handling case. Both essentially invoke Popen (). Wait () to await the end of the process and return to ReturnCode subprocess.check_output ()
Subprocess.check_output (args, *, Stdin=none, Stderr=none, Shell=false, Universal_newlines=false, Timeout=None)
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. last
In the use of subprocess module is very easy to die when the situation appears, some to avoid the idea of death, we will talk later. : -)