Home >Backend Development >Python Tutorial >Introduction and use of subprocess module

Introduction and use of subprocess module

零下一度
零下一度Original
2017-07-23 13:46:449559browse

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....

1. Introduction

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.*

## When running python, we They are all creating and running a process. In Linux, 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.

2. Use of old modules

1.os.system()

Execute operating system commands, Output the results to the screen and only return the command execution status (0: success, non-0: failure)

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 &#39;ls -l&#39;, mode &#39;r&#39; 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
 

3. subprocess module

1. subprocess.run()

>>> 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=[&#39;df&#39;, &#39;-h&#39;], returncode=0)

# 需要交给Linux shell自己解析,则:传入命令字符串,shell=True
>>> subprocess.run("df -h|grep /dev/sda1",shell=True)
/dev/sda1             283M   27M  241M  11% /boot
CompletedProcess(args=&#39;df -h|grep /dev/sda1&#39;, 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
0
 

3. 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 directory
 

4. 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(&#39;pwd&#39;)
(0, &#39;/root&#39;)

#执行错误
>>> subprocess.getstatusoutput(&#39;pd&#39;)
(127, &#39;/bin/sh: pd: command not found&#39;)
 

5. subprocess.getoutput()

Accept commands in the form of strings and put back the execution results

>>> subprocess.getoutput(&#39;pwd&#39;)
&#39;/root&#39;
 

6. subprocess.check_output()

Execute the command and return the execution result instead of printing

>>> res = subprocess.check_output("pwd")
>>> res
b&#39;/root\n&#39; # 结果以字节形式返回

4. subprocess.Popen()

In fact, the above methods used by subprocess are all encapsulation of subprocess.Popen. Let’s take a look. This Popen method.

1, stdout

Standard output

>>> res = subprocess.Popen("ls /tmp/yum.log", shell=True, stdout=subprocess.PIPE)  # 使用管道
>>> res.stdout.read()    # 标准输出
b&#39;/tmp/yum.log\n&#39;

res.stdout.close()   # 关闭

 

2、stderr

Standard error

>>> import subprocess
>>> res = subprocess.Popen("lm -l",shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
# 标准输出为空
>>> res.stdout.read()
b&#39;&#39;

#标准错误中有错误信息
>>> res.stderr.read()
b&#39;/bin/sh: lm: command not found\n&#39;

  

注意:上面的提到的标准输出都为啥都需要等于subprocess.PIPE,这个又是啥呢?原来这个是一个管道,这个需要画一个图来解释一下:

4、poll()

定时检查命令有没有执行完毕,执行完毕后返回执行结果的状态,没有执行完毕返回None

>>> res = subprocess.Popen("sleep 10;echo &#39;hello&#39;",shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
>>> print(res.poll())
None
>>> print(res.poll())
None
>>> print(res.poll())
0

  

5、wait()

等待命令执行完成,并且返回结果状态

>>> obj = subprocess.Popen("sleep 10;echo &#39;hello&#39;",shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
>>> obj.wait()


# 中间会一直等待


0

  

6、terminate()

结束进程

import subprocess

>>> res = subprocess.Popen("sleep 20;echo &#39;hello&#39;",shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
>>> res.terminate()  # 结束进程
>>> res.stdout.read() 
b&#39;&#39;

7、pid

获取当前执行子shell的程序的进程号

import subprocess

>>> res = subprocess.Popen("sleep 5;echo &#39;hello&#39;",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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn