subprocess包中定义有数个创建子进程的函数,这些函数分别以不同的方式创建子进程。在使用过程中,可以根据需要来从中选取一个使用。
另外subprocess还提供了一些管理标准流(standard stream)和管道(pipe)的工具,从而在进程间使用文本通信。
使用subprocess包的函数创建子进程的时候,要注意:
Ø 在创建子进程之后,父进程是否暂停,并等待子进程运行。
Ø 函数返回什么
Ø 当return code不为0时,父进程如何处理。
1 Popen
这个模块主要就提供一个类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)
参数介绍:
args | 字符串或者列表 |
bufsize | 0 无缓冲;1 行缓冲;其他正值 缓冲区大小 负值 采用默认系统缓冲(一般是全缓冲) |
executable | 一般不用,args字符串或列表第一项表示程序名 |
stdin stdout stderr | None 没有任何重定向,继承父进程 PIPE 创建管道,文件对象文件描述符(整数) |
preexec_fn | 钩子函数, 在fork和exec之间执行。(unix) |
close_fds | unix 下执行新进程前是否关闭0/1/2之外的文件 |
shell | 为真的话 window下,相当于添加"cmd.exe /c" |
cwd | 设置工作目录 |
env | 设置环境变量 |
universal_newlines | 各种换行符统一处理成 '\n' |
startupinfo | window下传递给CreateProcess的结构体 |
creationflags | windows下,传递CREATE_NEW_CONSOLE创建自己的控制台窗口 |
关于popen的使用方法,实际上仔细看《相关函数介绍》就可以
对象
poll() | 检查是否结束,设置返回值 |
wait() | 等待结束,设置返回值 |
communicate() | 参数是标准输入,返回标准输出和标准出错 |
send_signal() | 发送信号 (主要在unix下有用) |
terminate() | 终止进程,unix对应的SIGTERM信号,windows下调用api函数TerminateProcess() |
kill() | 杀死进程(unix对应SIGKILL信号),windows下同上 |
stdin stdout stderr | 参数中指定PIPE时,有用 |
pid | 进程id |
returncode | 进程返回值 |
2 相关函数介绍
2.1 subprocess.call()
语法:
subprocess.call(args, *, stdin=None, stdout=None, stderr=None, shell=False)
语义:
运行由args指定的命令,直到命令结束后;
返回 返回码的属性值,相当于exit code。
父进程等待子进程完成
实现:
def call(*popenargs, **kwargs): return Popen(*popenargs, **kwargs).wait()
用法:
>>> import subprocess >>> subprocess.call(["ls", "-l"]) 0 >>> subprocess.call("exit 1", shell=True)
注意事项:
(1) 使用 shell=True 是一种安全保护机制。
(2) 在使用这个函数时,不要使用 stdout=PIPE 或 stderr=PIPE 参数,不然会导致子进程输出的死锁。如果要使用管道,可以在 communicate()方法中使用Popen
import subprocess out = subprocess.call("ls -l", shell=True) out = subprocess.call("cd ..", shell=True)
2.2 subprocess.check_call()
语法:
subprocess.check_call(args, *, stdin=None, stdout=None, stderr=None, shell=False)
语义:
运行由args指定的命令,直到命令执行完成。
如果返回码为零,则返回;否则,抛出 CalledProcessError异常。
CalledProcessError对象包含有返回码的属性值,可用try...except...来检查。
父进程等待子进程完成
实现:
def check_call(*popenargs, **kwargs): retcode = call(*popenargs, **kwargs) if retcode: cmd = kwargs.get("args") raise CalledProcessError(retcode, cmd) return 0
示例:
上面显示的参数仅仅是最常见的,下面是用户更常用的参数。
>>> >>> subprocess.check_call(["ls", "-l"]) 0 >>> subprocess.check_call("exit 1", shell=True) Traceback (most recent call last): ... subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1
注意事项:
(1) 这个函数在python 2.5版本中引入。
(2) 使用 shell=True 是一种安全机制。
(3) 不要在这个函数中使用 stdout=PIPE 或 stderr=PIPE, 否则会造成子进程死锁。如果需要使用管道,可以在 communicate()方法中使用Popen.
2.3 subprocess.check_output()
语法:
subprocess.check_output(args, *, stdin=None, stderr=None, shell=False, universal_newlines=False)
语义:
运行args定义的命令,并返回一个字符串表示的输出值。
如果返回码为非零,则抛出 CalledProcessError异常。,可用try...except...来检查。
父进程等待子进程完成
实现:
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
示例:
>>> >>> subprocess.check_output(["echo", "Hello World!"]) 'Hello World!\n' >>> subprocess.check_output("exit 1", shell=True) Traceback (most recent call last): ... subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1
如果要捕捉结果中的标准错误,使用 stderr=subprocess.STDOUT参数:
>>> >>> subprocess.check_output( ... "ls non_existent_file; exit 0", ... stderr=subprocess.STDOUT, ... shell=True) 'ls: non_existent_file: No such file or directory\n'
注意事项:
(1) 这个函数在python 2.7版本中引入。
(2) WARNING: 使用 shell=True 是一种安全机制。
(3) 不要在这个函数中使用 stdout=PIPE 或 stderr=PIPE, 否则会造成子进程死锁。如果需要使用管道,可以在 communicate()方法中使用Popen.
2.4 常用操作
常用操作
import subprocess child = subprocess.Popen(["ping","-c","5","www.google.com"]) child.poll() # 检查子进程状态 child.kill() # 终止子进程 child.send_signal() # 向子进程发送信号 child.terminate() # 终止子进程
子进程的PID存储在child.pid
3 子进程的文本流控制
(沿用child子进程) 子进程的标准输入,标准输出和标准错误也可以通过如下属性表示:
child.stdin
child.stdout
child.stderr
可以在Popen()建立子进程的时候改变标准输入、标准输出和标准错误,并可以利用subprocess.PIPE将多个子进程的输入和输出连接在一起,构成管道(pipe):
import subprocess child1 = subprocess.Popen(["ls","-l"], stdout=subprocess.PIPE) child2 = subprocess.Popen(["wc"], stdin=child1.stdout,stdout=subprocess.PIPE) out = child2.communicate() print(out)
subprocess.PIPE实际上为文本流提供一个缓存区。child1的stdout将文本输出到缓存区,随后child2的stdin从该PIPE中将文本读取走。child2的输出文本也被存放在PIPE中,直到communicate()方法从PIPE中读取出PIPE中的文本。
要注意的是,communicate()是Popen对象的一个方法,该方法会阻塞父进程,直到子进程完成。
还可以利用communicate()方法来使用PIPE给子进程输入:
import subprocess child = subprocess.Popen(["cat"], stdin=subprocess.PIPE) child.communicate("vamei")
我们启动子进程之后,cat会等待输入,直到我们用communicate()输入"vamei"。
通过使用subprocess包,我们可以运行外部程序。这极大的拓展了Python的功能。如果你已经了解了操作系统的某些应用,你可以从Python中直接调用该应用(而不是完全依赖Python),并将应用的结果输出给Python,并让Python继续处理。shell的功能(比如利用文本流连接各个应用),就可以在Python中实现。