Home >Backend Development >Python Tutorial >Introduction and use of subprocess module
1. Subprocess and commonly used encapsulation functions
When running python, we are creating and running a process. Like a Linux process, a process can fork a child process and let the child process exec another program. In Python, we use the subprocess package in the standard library to fork a subprocess and run an external program.
The subprocess package defines several functions for creating subprocesses. These functions create subprocesses in different ways, so we can choose one of them to use according to our needs. In addition, subprocess also provides some tools for managing standard streams and pipes to use text communication between processes.
subprocess.call()
The parent process waits for the child process to complete
Return exit information (returncode, equivalent to Linux exit code)
subprocess.check_call()
Parent The process waits for the subprocess to complete
Return 0
Check the exit information. If the returncode is not 0, raise the error subprocess.CalledProcessError. This object contains the returncode attribute, which can be checked with try...except...
subprocess.check_output()
The parent process waits for the child process to complete
Return the output result of the subprocess to the standard output
Check the exit information, if the returncode is not 0, raise the error subprocess.CalledProcessError, this object Contains the returncode attribute and the output attribute. The output attribute is the output result of the standard output and can be checked with try...except....
Subprocess was first introduced in version 2.4. Used to spawn child processes, connect their input/output/errors through pipes, and obtain their return values.
Subprocess is used to replace multiple old modules and functions:
os.system
os.spawn*
os.popen*
popen2.*
commands.*
import os >>> a = os.system("df -Th") Filesystem Type Size Used Avail Use% Mounted on /dev/sda3 ext4 1.8T 436G 1.3T 26% / tmpfs tmpfs 16G 0 16G 0% /dev/shm /dev/sda1 ext4 190M 118M 63M 66% /boot >>> a 0 # 0 表示执行成功 # 执行错误的命令 >>> res = os.system("list") sh: list: command not found >>> res 32512 # 返回非 0 表示执行错误2. os.popen()
Execute the command of the operating system and the result will be saved in the memory, which can be read out using the read() method
import os >>> res = os.popen("ls -l") # 将结果保存到内存中 >>> print res <open file 'ls -l', mode 'r' at 0x7f02d249c390> # 用read()读取内容 >>> print res.read() total 267508 -rw-r--r-- 1 root root 260968 Jan 27 2016 AliIM.exe -rw-------. 1 root root 1047 May 23 2016 anaconda-ks.cfg -rw-r--r-- 1 root root 9130958 Nov 18 2015 apache-tomcat-8.0.28.tar.gz -rw-r--r-- 1 root root 0 Oct 31 2016 badblocks.log drwxr-xr-x 5 root root 4096 Jul 27 2016 certs-build drwxr-xr-x 2 root root 4096 Jul 5 16:54 Desktop -rw-r--r-- 1 root root 2462 Apr 20 11:50 Face_24px.ico
>>> import subprocess # python 解析则传入命令的每个参数的列表 >>> subprocess.run(["df","-h"]) Filesystem Size Used Avail Use% Mounted on /dev/mapper/VolGroup-LogVol00 289G 70G 204G 26% / tmpfs 64G 0 64G 0% /dev/shm /dev/sda1 283M 27M 241M 11% /boot CompletedProcess(args=['df', '-h'], returncode=0) # 需要交给Linux shell自己解析,则:传入命令字符串,shell=True >>> subprocess.run("df -h|grep /dev/sda1",shell=True) /dev/sda1 283M 27M 241M 11% /boot CompletedProcess(args='df -h|grep /dev/sda1', returncode=0)2 , subprocess.call()Execute the command and return the result and execution status of the command, 0 or non-0
>>> res = subprocess.call(["ls","-l"]) 总用量 28 -rw-r--r-- 1 root root 0 6月 16 10:28 1 drwxr-xr-x 2 root root 4096 6月 22 17:48 _1748 -rw-------. 1 root root 1264 4月 28 20:51 anaconda-ks.cfg drwxr-xr-x 2 root root 4096 5月 25 14:45 monitor -rw-r--r-- 1 root root 13160 5月 9 13:36 npm-debug.log # 命令执行状态 >>> res 03. subprocess. check_call()Execute the command and return the result and status. Normally it is 0. If there is an execution error, an exception will be thrown.
>>> subprocess.check_call(["ls","-l"]) 总用量 28 -rw-r--r-- 1 root root 0 6月 16 10:28 1 drwxr-xr-x 2 root root 4096 6月 22 17:48 _1748 -rw-------. 1 root root 1264 4月 28 20:51 anaconda-ks.cfg drwxr-xr-x 2 root root 4096 5月 25 14:45 monitor -rw-r--r-- 1 root root 13160 5月 9 13:36 npm-debug.log 0 >>> subprocess.check_call(["lm","-l"]) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib64/python2.7/subprocess.py", line 537, in check_call retcode = call(*popenargs, **kwargs) File "/usr/lib64/python2.7/subprocess.py", line 524, in call return Popen(*popenargs, **kwargs).wait() File "/usr/lib64/python2.7/subprocess.py", line 711, in __init__ errread, errwrite) File "/usr/lib64/python2.7/subprocess.py", line 1327, in _execute_child raise child_exception OSError: [Errno 2] No such file or directory4. subprocess. getstatusoutput()
Accepts a command in the form of a string and returns a result in the form of a tuple. The first element is the command execution status, and the second element is the execution result
#执行正确 >>> subprocess.getstatusoutput('pwd') (0, '/root') #执行错误 >>> subprocess.getstatusoutput('pd') (127, '/bin/sh: pd: command not found')5. subprocess.getoutput()
Accept commands in the form of strings and put back the execution results
>>> subprocess.getoutput('pwd') '/root'6. subprocess.check_output()Execute the command and return the execution result instead of printing
>>> res = subprocess.check_output("pwd") >>> res b'/root\n' # 结果以字节形式返回
In fact, the above methods used by subprocess are all encapsulation of subprocess.Popen. Let’s take a look. This Popen method.
Standard output
>>> res = subprocess.Popen("ls /tmp/yum.log", shell=True, stdout=subprocess.PIPE) # 使用管道 >>> res.stdout.read() # 标准输出 b'/tmp/yum.log\n' res.stdout.close() # 关闭2、stderrStandard error
>>> import subprocess >>> res = subprocess.Popen("lm -l",shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE) # 标准输出为空 >>> res.stdout.read() b'' #标准错误中有错误信息 >>> res.stderr.read() b'/bin/sh: lm: command not found\n'
注意:上面的提到的标准输出都为啥都需要等于subprocess.PIPE,这个又是啥呢?原来这个是一个管道,这个需要画一个图来解释一下:
定时检查命令有没有执行完毕,执行完毕后返回执行结果的状态,没有执行完毕返回None
>>> res = subprocess.Popen("sleep 10;echo 'hello'",shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE) >>> print(res.poll()) None >>> print(res.poll()) None >>> print(res.poll()) 0
等待命令执行完成,并且返回结果状态
>>> obj = subprocess.Popen("sleep 10;echo 'hello'",shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE) >>> obj.wait() # 中间会一直等待 0
结束进程
import subprocess >>> res = subprocess.Popen("sleep 20;echo 'hello'",shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE) >>> res.terminate() # 结束进程 >>> res.stdout.read() b''
获取当前执行子shell的程序的进程号
import subprocess >>> res = subprocess.Popen("sleep 5;echo 'hello'",shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE) >>> res.pid # 获取这个linux shell 的 进程号 2778
The above is the detailed content of Introduction and use of subprocess module. For more information, please follow other related articles on the PHP Chinese website!