python subprocess模块

从python2.4开始,python引入subprocess模块来管理子进程,以取代一些旧模块的方法:如 os.system、os.spawn*、os.popen*、popen2.*、commands.*。 subprocess模块不但可以调用外部的命令作为子进程,而且可以连接到子进程的input/output/error管道,获取相关的返回信息。

subprocess以及常用的封装函数

运行python的时候,我们都是在创建并运行一个进程。像Linux进程那样,一个进程可以fork一个子进程,并让这个子进程exec另外一个程序。在Python中,我们通过标准库中的subprocess包来fork一个子进程,并运行一个外部的程序。

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

subprocess.call() :父进程等待子进程完成,返回退出信息(returncode,相当于Linux exit code);

subprocess.check_call():父进程等待子进程完成,检查退出信息,如果returncode不为0,则举出错误subprocess.CalledProcessError,该对象包含有returncode属性,可用try...except...来检查;

subprocess.check_output(): 父进程等待子进程完成, 返回子进程向标准输出的输出结果. 检查退出信息,如果returncode不为0,则举出错误subprocess.CalledProcessError,该对象包含有returncode属性和output属性,output属性为标准输出的输出结果,可用try...except...来检查;

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

>>> import subprocess
>>> rc = subprocess.call(["ls","-l"])
总用量 40
drwxrwxr-x  4 fc fc 4096 Dec  5 17:42 data
drwxrwxr-x 10 fc fc 4096 Dec  5 13:57 docs
-rw-rw-r--  1 fc fc 1087 Dec  5 13:57 LICENSE
-rw-rw-r--  1 fc fc 1343 Dec  5 13:57 readme.md
-rw-rw-r--  1 fc fc   50 Dec  5 13:57 readthedocs.yml
-rw-rw-r--  1 fc fc  898 Dec  5 13:57 setup.py
drwxrwxr-x  7 fc fc 4096 Dec  5 13:58 spinup
drwxrwxr-x  2 fc fc 4096 Dec  5 13:58 spinup.egg-info
drwxrwxr-x  2 fc fc 4096 Dec  5 13:57 test
-rwxrwxr-x  1 fc fc 1155 Dec  5 13:57 travis_setup.sh
>>> print(rc)
0

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

shell默认为False,在Linux下,shell=False时, Popen调用os.execvp()执行args指定的程序;shell=True时,如果args是字符串,Popen直接调用系统的Shell来执行args指定的程序,如果args是一个序列,则args的第一项是定义程序命令字符串,其它项是调用系统Shell时的附加参数。上面例子也可以写成如下:

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

在Windows下,不论shell的值如何,Popen调用CreateProcess()执行args指定的外部程序。如果args是一个序列,则先用list2cmdline()转化为字符串,但需要注意的是,并不是MS Windows下所有的程序都可以用list2cmdline来转化为命令行字符串。

subprocess.Popen

class Popen( args, bufsize=-1, executable=None, stdin=None, stdout=None, 
stderr=None, preexec_fn=None, close_fds=<object object at 0x7fd4d20d90d0>, 
shell=False, cwd=None, env=None, universal_newlines=False, startupinfo=None,
creationflags=0, restore_signals=True, start_new_session=False, pass_fds=())

实际上,上面列出的几个函数都是基于Popen()的封装(wrapper)。这些封装的目的在于让我们容易使用子进程。当我们想要更个性化我们的需求的时候,就要转向Popen类,该类生成的对象用来代表子进程。

与上面的封装不同,Popen对象创建后,主程序不会自动等待子进程完成。我们必须调用对象的wait()方法,父进程才会等待 (也就是阻塞block),举例:

>>> child = subprocess.Popen(['ping','-c','4','linux.linuxidc.com']); print('parent process')
parent process
>>> PING linux.linuxidc.com (60.191.177.106) 56(84) bytes of data.

--- linux.linuxidc.com ping statistics ---
4 packets transmitted, 0 received, 100% packet loss, time 3058ms

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

对比等待的情况:

>>> child = subprocess.Popen(['ping','-c','4','linux.linuxidc.com']); child.wait();print('parent process')
PING linux.linuxidc.com (60.191.177.106) 56(84) bytes of data.

--- linux.linuxidc.com ping statistics ---
4 packets transmitted, 0 received, 100% packet loss, time 3055ms

1
parent process

从运行结果中看到,父进程在开启子进程之后并等待child的完成后,再运行print。

此外,你还可以在父进程中对子进程进行其它操作,比如我们上面例子中的child对象:

  1. child.poll()           # 检查子进程状态
  2. child.kill()           # 终止子进程
  3. child.send_signal()    # 向子进程发送信号
  4. child.terminate()      # 终止子进程
  5. 子进程的PID存储在child.pid

子进程的文本流控制

子进程的标准输入、标准输出和标准错误如下属性分别表示:

  1. child.stdin
  2. child.stdout
  3. child.stderr

可以在Popen()建立子进程的时候改变标准输入、标准输出和标准错误,并可以利用subprocess.PIPE将多个子进程的输入和输出连接在一起,构成管道(pipe),如下2个例子:

>>> child1 = subprocess.Popen(["ls","-l"], stdout=subprocess.PIPE)
>>> print(child1.stdout.read())
b'\xe6\x80\xbb\xe7\x94\xa8\xe9\x87\x8f 40\ndrwxrwxr-x  4 fc fc 4096 Dec  5 17:42 data\ndrwxrwxr-x 10 fc fc 4096 Dec  5 13:57 docs\n-rw-rw-r--  1 fc fc 1087 Dec  5 13:57 LICENSE\n-rw-rw-r--  1 fc fc 1343 Dec  5 13:57 readme.md\n-rw-rw-r--  1 fc fc   50 Dec  5 13:57 readthedocs.yml\n-rw-rw-r--  1 fc fc  898 Dec  5 13:57 setup.py\ndrwxrwxr-x  7 fc fc 4096 Dec  5 13:58 spinup\ndrwxrwxr-x  2 fc fc 4096 Dec  5 13:58 spinup.egg-info\ndrwxrwxr-x  2 fc fc 4096 Dec  5 13:57 test\n-rwxrwxr-x  1 fc fc 1155 Dec  5 13:57 travis_setup.sh\n'

#或者child1.communicate(),communicate()是Popen对象的一个方法,该方法会阻塞父进程,直到子进程完成。

>>> child1 = subprocess.Popen(["cat","/etc/passwd"], stdout=subprocess.PIPE)
>>> child2 = subprocess.Popen(["grep","0:0"], stdin=child1.stdout, stdout=subprocess.PIPE)
>>> out = child2.communicate()
>>> child1.stdout.read()
b''

subprocess.PIPE实际上为文本流提供一个缓存区。child1的stdout将文本输出到缓存区,随后child2的stdin从该PIPE中将文本读取走。child2的输出文本也被存放在PIPE中,直到communicate()方法从PIPE中读取出PIPE中的文本。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值