python 标准库subprocess

作者:Vamei

出处:http://www.cnblogs.com/vamei

subprocess包主要功能是执行外部的命令和程序。subprocess的功能与shell类似。
subprocess包中定义有数个创建子进程的函数,这些函数分别以不同的方式创建子进程,所以我们可以根据需要来从中选取一个使用。
另外subprocess还提供了一些管理标准流(standard stream)和管道(pipe)的工具,从而在进程间使用文本通信。

使用subprocess包中的函数创建子进程的时候,要注意:
1) 在创建子进程之后,父进程是否暂停,并等待子进程运行
2) 函数返回什么
3) 当returncode不为0时,父进程如何处理

subprocess.Popen()

Popen用来创建子进程与父进程并行执行,默认父进程不等待新进程结束。
我们必须调用对象的wait()方法,父进程才会等待 (也就是阻塞block)。

import subprocess
child= subprocess.Popen(["ping","-c","5","www.google.com"])
printf("parent process")

从运行结果中看到,父进程在开启子进程之后并没有等待child的完成,而是直接运行print。
对比等待的情况:

import subprocess
child= subprocess.Popen(["ping","-c","5","www.google.com"])
child.wait()
print("parent process")

父进程中对子进程进行其它操作,比如我们上面例子中的child对象:

  • child.poll():检查子进程状态
  • child.kill():终止子进程
  • child.send_signal(): 向子进程发送信号
  • child.terminate(): 终止子进程
  • child.pid:子进程的PID

子进程的文本流控制
子进程的标准输入,标准输出和标准错误也可以通过如下属性表示:

  • 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中的文本。

操作stdout与stdin

import subprocess
child1= subprocess.Popen(["ping","-n","5","sina.com.cn"], stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
out =child1.stdout.read().decode('cp936')
print(out)

# for line in child1.stdout.readlines():
#         output = line.decode('cp936')
#         print("%s\n" % output)
正在 Ping sina.com.cn [123.126.55.41] 具有 32 字节的数据:
来自 123.126.55.41 的回复: 字节=32 时间=40ms TTL=49
来自 123.126.55.41 的回复: 字节=32 时间=40ms TTL=49
来自 123.126.55.41 的回复: 字节=32 时间=44ms TTL=49
来自 123.126.55.41 的回复: 字节=32 时间=41ms TTL=49
来自 123.126.55.41 的回复: 字节=32 时间=40ms TTL=49

123.126.55.41 的 Ping 统计信息:
    数据包: 已发送 = 5,已接收 = 5,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
    最短 = 40ms,最长 = 44ms,平均 = 41ms

 

 

communicate()

是Popen对象的一个方法,该方法会阻塞父进程,直到子进程完成,communicate可以与新进程交互,但是必须要在popen构造时候将管道重定向。

def TestCommunicate():
  import subprocess
  cmd = "dir"
  p=subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  (stdoutdata, stderrdata) = p.communicate()
 
  if p.returncode != 0:
        print (cmd + "error !")
  #defaultly the return stdoutdata is bytes, need convert to str and cp936
  for r in str(stdoutdata,encoding='cp936' ).split("\n"):
    print (r)
  print (p.returncode)


def TestCommunicate2():
  import subprocess
  cmd = "dir"
  #universal_newlines=True, it means by text way to open stdout and stderr
  p = subprocess.Popen(cmd, shell=True, universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  curline = p.stdout.readline()

  while(curline != ""):
        print (curline)
        curline = p.stdout.readline()
  p.wait()
  print (p.returncode)

 

 subprocess.call()、subprocess.check_call()、subprocess.check_output()

subprocess.call()

父进程等待子进程完成

返回退出信息(returncode,相当于exit code,见Linux进程基础)

subprocess.check_call()

父进程等待子进程完成

返回0

检查退出信息,如果returncode不为0,则举出错误subprocess.CalledProcessError,该对象包含有returncode属性,可用try...except...来检查(见Python错误处理)。

subprocess.check_output()

父进程等待子进程完成

返回子进程向标准输出的输出结果

检查退出信息,如果returncode不为0,则举出错误subprocess.CalledProcessError,该对象包含有returncode属性和output属性,output属性为标准输出的输出结果,可用try...except...来检查。

这三个函数的使用方法相类似,我们以subprocess.call()来说明:

import subprocess

rc = subprocess.call(["ls","-l"])

我们将程序名(ls)和所带的参数(-l)一起放在一个表中传递给subprocess.call()

可以通过一个shell来解释一整个字符串:

importsubprocess

out= subprocess.call("ls -l", shell=True)

out= subprocess.call("cd ..", shell=True)

我们使用了shell=True这个参数。这个时候,我们使用一整个字符串,而不是一个表来运行子进程。Python将先运行一个shell,再用这个shell来解释这整个字符串。

shell命令中有一些是shell的内建命令,这些命令必须通过shell运行,$cd。shell=True允许我们运行这样一些命令。

posted @ 2019-12-15 21:18  -零  阅读(733)  评论(0编辑  收藏  举报