The subprocess in Python. Popen () use

Source: Internet
Author: User
Tags posix pkill dmesg

Starting with the python2.4 version, you can use the Subprocess module to generate child processes and connect to the child process's standard input/output/error, as well as to get the return value of the child process.
Subprocess is intended to replace several other older modules or functions, such as: Os.system os.spawn* os.popen* popen2.* commands.*

First, subprocess. Popen
The Subprocess module defines a class: Popen
Class subprocess. 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)


Each parameter has the following meanings:

Args
The args parameter. Can be a string that can be a list that contains program parameters. The program to execute is usually the first item in the list, or the string itself.
Subprocess. Popen (["Cat", "test.txt"])
Subprocess. Popen ("Cat test.txt")
These two, the latter will not work. Because if it is a string, it must be the path of the program. (Consider the UNIX API function exec, which accepts a string
List
But the following can work
Subprocess. Popen ("Cat test.txt", shell=true)
This is because it is equivalent to
Subprocess. Popen (["/bin/sh", "-C", "Cat Test.txt"])
Under *nix, when Shell=false (the default), Popen uses OS.EXECVP () to execute the subroutine. Args are generally a "list". If Args is a string,
Can be used as the path to the executable file so that no parameters can be passed in.

Attention:
Shlex.split () can be used to serialize complex command parameters, such as:
>>> shlex.split (' ls PS top grep pkill ')
[' ls ', ' ps ', ' top ', ' grep ', ' Pkill ']
>>>import Shlex, subprocess
>>>command_line = Raw_input ()
/bin/cat-input test.txt-output "Diege.txt"-cmd "Echo ' $MONEY '"
>>>args = Shlex.split (command_line)
>>> Print args
['/bin/cat ', '-input ', ' test.txt ', '-output ', ' diege.txt ', '-cmd ', ' echo ' $MONEY ']
>>>p=subprocess. Popen (args)
As you can see, space-delimited options (such as-input) and parameters (such as test.txt) are split into separate items in the list, but the enclosed or escaped spaces are not
。 This is a bit like the behavior of most shells.

Under *nix, when Shell=true, if Arg is a string, use the shell to explain the execution of the string. If args is a list, the first item is treated as a command,
The rest is considered a parameter to the shell itself. In other words, it is equivalent to:
Subprocess. Popen (['/bin/sh ', '-C ', args[0], args[1], ...])

Under Windows, the following can work
Subprocess. Popen (["Notepad.exe", "test.txt"])
Subprocess. Popen ("notepad.exe test.txt")
This is because the API function under Windows CreateProcess accepts a string. Even parameters in the form of a list need to be merged into a string before being passed to the API function
Subprocess. Popen ("notepad.exe test.txt" shell=true)
Equivalent to
Subprocess. Popen ("cmd.exe/c" + "notepad.exe test.txt" shell=true)

BufSize Parameters:
If the bufsize parameter is specified, it is the same as the built-in function open (): 0 for non-buffering, 1 for row buffering, other positive for approximate buffer bytes, negative table
Use the system default values. The default is 0.

Executable Parameters:
Specifies the program to execute. It is seldom used: a generic program can be specified by the args parameter. If shell=true, executable
Can be used to specify which shell to execute (such as bash, CSH, zsh, and so on). *nix, the default is/bin/sh, under Windows, is the environment variable COMSPEC
The value. Under Windows, you only need to specify shell=true if the command you want to execute is actually a shell built-in command (such as dir, copy, etc.)
, and you do not need to specify this when you want to execute a command-line-based batch script.

stdin stdout and stderr:
stdin stdout and stderr, respectively, represent standard input, standard output, and standard errors for subroutines. The optional value is a pipe or a valid file descriptor (in fact, a positive
Integer) or a file object, and none. If it is a pipe, it means that a new pipe needs to be created, if none
, no redirection work is done, and the child process's file descriptor inherits from the parent process. In addition, the value of stderr can also be stdout
, the standard error that represents the child process is also output to standard output.

PREEXEC_FN Parameters:
If the PREEXEC_FN is set to a callable object (such as a function), it is called before the child process is executed. (*nix only)

Close_fds Parameters:
If the Close_fds is set to True,*nix, the file descriptors except 0, 1, 2 will be closed before the process is opened. Other file descriptors are not inherited under Windows.

Shell parameters:
If the shell is set to true, the specified command is interpreted and executed in the shell.

CWD Parameters:
If CWD is not none, CWD will be the current directory of the subroutine. Note that this directory will not be the search directory for the executable file, so do not place the program file
The directory is set to CWD.

ENV parameter:
If Env is not none, the subroutine's environment variable is set by the value of Env instead of inheriting the parent process's environment variable by default. Note that even if you define it only in Env
The value of an environment variable also prevents the subroutine from getting its
The environment variables of his parent process (that is, if there are only 1 items in Env, the child process has only 1 environment variables). For example:

>>> subprocess. Popen (' env ', env={' test ': ' 123 ', ' testtext ': ' zzz '})
Test=123
<subprocess. Popen Object at 0x2870ad2c>
Testtext=zzz

Universal_newlines Parameters:
If Universal_newlines is set to true, the stdout and stderr of the child process are treated as text objects, and the line terminator of the *nix ('/n '
), either the line terminator ('/R ') in the old Mac format, or the line terminator ('/r/n ') in Windows format will be treated as '/n '.

Startupinfo and Creationflags parameters:
If Startupinfo and creationflags are specified, they are passed to the following CreateProcess () function, which specifies various other properties of the subroutine, such as the main window style or the
The priority of the child process, and so on. (Windows only)

Second, subprocess. PIPE
Subprocess. PIPE
A stdin, stdout, and stderr 3 parameters that can be used for popen, indicating that a new pipeline needs to be created.
Subprocess. STDOUT
An output value that can be used for the stderr parameter of the Popen, which represents the standard error of the subroutine converging to the standard output.
Instance:
>>>p=subprocess. Popen ("Df-h", shell=true,stdout=subprocess. PIPE)
>>>out=p.stdout.readlines ()
>>>out
[b ' Filesystem Size used Avail capacity mounted on\n ', B '/dev/ad0s1a 713M 313M 343M 48%/\n ', B ' de VFS 1.0K 1.0K 0B 100%/dev\n ', b '/dev/ad0s1e 514M 2.1M 471M 0%/tmp\n ', B '/dev/ad0s1 F 4.3G 2.5G 1.4G 64%/usr\n ', b '/dev/ad0s1d 2.0G 121M 1.7G 6%/var\n '
>>> for line in out:
... print line.strip ()
...
Filesystem Size used Avail capacity mounted on
/DEV/AD0S1A 713M 313M 343M 48%/
Devfs 1.0K 1.0K 0B 100%/dev
/dev/ad0s1e 514M 2.1M 471M 0%/tmp
/dev/ad0s1f 4.3G 2.5G 1.4G 64%/usr
/dev/ad0s1d 2.0G 121M 1.7G 6%/var
StdOut can use Read (), ReadLine (), ReadLines () and other methods

Third, the convenient function
1, Subprocess.call
Subprocess.call (*popenargs, **kwargs)
Executes the command, waits for the command to end, and then returns the return value of the child process. Parameters with Popen, view/usr/lib/python2.7/subprocess.py
Remove the document, in fact, this is:
def call (*popenargs, **kwargs):
Return Popen (*popenargs, **kwargs). Wait ()
>>> subprocess.call (' ifconfig ', shell=true)

2, Subprocess.check_call
Subprocess.check_call (*popenargs, **kwargs)
Executes the call command above and checks the return value, if the child process returns a non-0, the Calledprocesserror exception will be thrown, and the exception will have a ReturnCode
property to record the return value of the child process.
def check_call (*popenargs, **kwargs):
Retcode = Call (*popenargs, **kwargs)
If Retcode:
cmd = Kwargs.get ("args")
Raise Calledprocesserror (Retcode, cmd)
return 0
>>> subprocess.check_call (' ifconfig ')
>>> subprocess.call (' Noifconfig ')
Traceback (most recent):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/subprocess.py", line 493, on call
Return Popen (*popenargs, **kwargs). Wait ()
File "/usr/local/lib/python2.7/subprocess.py", line 679, in __init__
Errread, Errwrite)
File "/usr/local/lib/python2.7/subprocess.py", line 1228, in _execute_child
Raise Child_exception
OSError: [Errno 2] No such file or directory
Exceptions thrown in the exception subprocess are thrown again in the parent process. Also, the exception will have an extra property called Child_traceback, which is a child process error traceback
The string of information. Encountering the most error back is OSError, such as executing a nonexistent subroutine that generates OSError. Also, if you call Popen using the wrong arguments
, the ValueError will be thrown. Check_call () also produces calledprocesserror exceptions when the subroutine returns non-0 o'clock.
Security
Unlike other popen functions, this function does not call/bin/sh to interpret the command, which means that each character in the command is safely passed to the child process.

3, Check_output
Check_output () executes the program and returns its standard output.
def check_output (*popenargs, **kwargs):
Process = Popen (*popenargs, Stdout=pipe, **kwargs)
Output, Unused_err = Process.communicate ()
Retcode = Process.poll ()
If Retcode:
cmd = Kwargs.get ("args")
Raise Calledprocesserror (Retcode, cmd, output=output)
Return output
P=subprocess.check_output (' ifconfig ')
The result is a string of all rows/n splits
You can print it directly.
Start Here

4. Popen Object


Generating objects
P=subprocess. Popen ("Df-h", shell=true,stdout=subprocess. PIPE)
>>> dir (P)

The Popen object has the following methods:

Popen.poll ()
Checks whether the child process has ended, sets and returns the ReturnCode property.

>>> P.poll ()
0

Popen.wait ()
Waits for the child process to end, sets and returns the ReturnCode property.
>>> p.wait ()
0
Note: If the subprocess outputs a large amount of data to the STDOUT or stderr pipeline and reaches the system pipe cache size,
The child process waits for the parent process to read the pipeline, and the parent process is waiting at this point, which will produce a legendary deadlock, with a very serious drop. Recommended Use
Communicate () to avoid the occurrence of this situation.

Popen.communicate (Input=none)
Interact with child processes: Send data to stdin, and read data from stdout and stderr until EOF is received. Wait for the child process to end. The optional input, if any, is the string type.
This function returns a tuple: (Stdoutdata, Stderrdata).
Note that in order to send data to the stdin of the child process, the stdin is to be a pipe when popen, and, similarly, to be able to receive data, stdout or stderr should also be a pipe.
P1=subprocess. Popen (' cat/etc/passwd ', shell=true,stdin=subprocess. Pipe,stdout=subprocess. PIPE)
>>> p2=subprocess. Popen (' grep 0:0 ', shell=true,stdin=p1.stdout,stdout=subprocess. PIPE)
Note: The data read will be cached in memory, so be careful when the amount of data is very large.
>>> P.communicate ()
(b ' Filesystem Size used Avail capacity mounted on\n/dev/ad0s1a 713M 313M 343M 48%/\NDEVFS    1.0K 1.0K 0B 100%/dev\n/dev/ad0s1e 514M 2.1M 471M 0%/tmp\n/dev/ad0s1f 4.3G 2.5G 1.4G 64%/usr\n/dev/ad0s1d 2.0G 121M 1.7G 6%/var\n ', None)

Popen.send_signal (signal)
Sends a signal signal to the child process.
Note: Only send Sigterm is currently supported under Windows, equivalent to the following terminate ().

Popen.terminate ()
Stops the child process. POSIX is the sending of sigterm signals. Windows is called TerminateProcess () this API.

Popen.kill ()
Kills a child process. POSIX is the sending of sigkill signals. Windows and terminate () are the same.

Popen.stdin
If the stdin parameter is pipe, this property is a file object, otherwise none.

Popen.stdout
If the stdout parameter is pipe, this property is a file object, otherwise none.

Popen.stderr
If the stderr parameter is pipe, this property is a file object, otherwise none.

Popen.pid
The process number of the child process. Note that if the shell parameter is true, this property refers to the child shell's process number.
>>> P.pid
22303

Popen.returncode
The return value of the subroutine, set by poll () or wait (), is also indirectly set by communicate ().
If none, indicates that the child process has not been terminated.
If negative-n indicates that the child process is terminated by the n signal. (*nux only)

Use subprocess to replace other functions
Can be done with subprocess, we assume that the module is imported with "from subprocess import *":

Instead of the shell command:
p= ' Ls-l '
is equivalent to
P=popen ([' ls ', '-l '],stdout=pipe). Communicate () [0]

Instead of the shell pipeline:
p= ' DMESG | grep CPU '
is equivalent to
P1=popen ([' DMESG '],stdout=pipe)
P2=popen ([' grep ', ' CPU '],stdin=p1.stdout,stdout=pipe)
Output = P2.communicate () [0]
Output
Cpu0: &LT;ACPI cpu> on ACPI0\NACPI_THROTTLE0: <acpi CPU throttling> on cpu0\n

>>> p1=subprocess. Popen (' cat/etc/passwd ', shell=true,stdout=subprocess. PIPE)
>>> p2=subprocess. Popen (' grep 0:0 ', shell=true,stdin=p1.stdout,stdout=subprocess. PIPE)
>>> p3=subprocess. Popen ("cut-d ': '-F 7", shell=true,stdin=p2.stdout,stdout=subprocess. PIPE)
>>> Print P3.stdout.read ()

Instead of Os.system ()
LSL = Os.system (' ls ' + '-l ')
This is a return state
is equivalent to
P=popen (' ls-l ', shell=true)
Lsl=os.waitpid (p.pid,0) [1]

Attention:
It is not usually necessary to invoke the program with the shell. It is easier to get the return value of the subroutine with subprocess.
In fact, a more realistic replacement is:
Try
Retcode = Call ("myCMD" + "Myarg", Shell=true)
If Retcode < 0:
Print >>sys.stderr, "Child is terminated by signal",-retcode
Else
Print >>sys.stderr, "Child returned", Retcode
Except OSError, E:
Print >>sys.stderr, "Execution failed:", E

Instead of the Os.spawn series
Examples of p_nowait
PID = OS.SPAWNLP (OS. P_nowait, "/bin/mycmd", "myCMD", "Myarg")
is equivalent to
PID = Popen (["/bin/mycmd", "Myarg"]). PID

Examples of p_wait

Retcode = OS.SPAWNLP (OS. P_wait, "/bin/mycmd", "myCMD", "Myarg")
is equivalent to
Retcode = Call (["/bin/mycmd", "Myarg"])

return value handling:

Pipe = Os.popen ("cmd", ' W ')
...
rc = Pipe.close ()
If RC! = None and RC% 256:
Print "There were some errors"
is equivalent to
Process = Popen ("cmd", ' W ', Shell=true, Stdin=pipe)
...
Process.stdin.close ()
If process.wait ()! = 0:
Print "There were some errors"

The subprocess in Python. Popen () use

Related Article

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.