Table of Contents
1. Introduction" >1. Introduction
" >2. Use of old modules
" > 3. subprocess module
" >1. subprocess.run()
" >4. subprocess.Popen()
" >1, stdout
4、poll()
5、wait()
6、terminate()
7、pid
Home Backend Development Python Tutorial Introduction and use of subprocess module

Introduction and use of subprocess module

Jul 23, 2017 pm 01:46 PM
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....

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 表示执行错误
Copy after login
 

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
Copy after login
 

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=['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)
Copy after login
 

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
Copy after login
 

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
Copy after login
 

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('pwd')
(0, '/root')

#执行错误
>>> subprocess.getstatusoutput('pd')
(127, '/bin/sh: pd: command not found')
Copy after login
 

5. subprocess.getoutput()

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

>>> subprocess.getoutput('pwd')
'/root'
Copy after login
 

6. subprocess.check_output()

Execute the command and return the execution result instead of printing

>>> res = subprocess.check_output("pwd")
>>> res
b'/root\n' # 结果以字节形式返回
Copy after login

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'/tmp/yum.log\n'

res.stdout.close()   # 关闭
Copy after login

 

2、stderr

Standard 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'
Copy after login

  

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

4、poll()

定时检查命令有没有执行完毕,执行完毕后返回执行结果的状态,没有执行完毕返回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
Copy after login

  

5、wait()

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

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


# 中间会一直等待


0
Copy after login

  

6、terminate()

结束进程

import subprocess

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

7、pid

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

import subprocess

>>> res = subprocess.Popen("sleep 5;echo 'hello'",shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
>>> res.pid  # 获取这个linux shell 的 进程号
2778
Copy after login

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 of this Website
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

Hot Article

Hot Article

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

WLAN expansion module has stopped [fix] WLAN expansion module has stopped [fix] Feb 19, 2024 pm 02:18 PM

WLAN expansion module has stopped [fix]

WLAN extensibility module cannot start WLAN extensibility module cannot start Feb 19, 2024 pm 05:09 PM

WLAN extensibility module cannot start

Python commonly used standard libraries and third-party libraries 2-sys module Python commonly used standard libraries and third-party libraries 2-sys module Apr 10, 2023 pm 02:56 PM

Python commonly used standard libraries and third-party libraries 2-sys module

Python programming: Detailed explanation of the key points of using named tuples Python programming: Detailed explanation of the key points of using named tuples Apr 11, 2023 pm 09:22 PM

Python programming: Detailed explanation of the key points of using named tuples

How to use DateTime in Python How to use DateTime in Python Apr 19, 2023 pm 11:55 PM

How to use DateTime in Python

How does Python's import work? How does Python's import work? May 15, 2023 pm 08:13 PM

How does Python's import work?

Detailed explanation of how Ansible works Detailed explanation of how Ansible works Feb 18, 2024 pm 05:40 PM

Detailed explanation of how Ansible works

This article summarizes the classic methods and effect comparison of feature enhancement & personalization in CTR estimation. This article summarizes the classic methods and effect comparison of feature enhancement & personalization in CTR estimation. Dec 15, 2023 am 09:23 AM

This article summarizes the classic methods and effect comparison of feature enhancement & personalization in CTR estimation.

See all articles