python 中 subprocess.Popen 总结

2023-05-16

python 中 subprocess.Popen 总结

subprocess的目的就是启动一个新的进程并且与之通信。
subprocess模块中只定义了一个类: Popen。可以使用Popen来创建进程,并与进程进行复杂的交互。它的构造函数如下:

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可以是字符串或者序列类型(如:list,元组),用于指定进程的可执行文件及其参数。如果是序列类型,第一个元素通常是可执行文件的路径。我们也可以显式的使用executeable参数来指定可执行文件的路径。

参数stdin, stdout, stderr分别表示程序的标准输入、输出、错误句柄。他们可以是PIPE,文件描述符或文件对象,也可以设置为None,表示从父进程继承。

如果参数shell设为true,程序将通过shell来执行。

参数env是字典类型,用于指定子进程的环境变量。如果env = None,子进程的环境变量将从父进程中继承。

subprocess.PIPE
  在创建Popen对象时,subprocess.PIPE可以初始化stdin, stdoutstderr参数。表示与子进程通信的标准流。

subprocess.STDOUT
  创建Popen对象时,用于初始化stderr参数,表示将错误通过标准输出流输出。

Popen.wait()
  等待子进程结束。设置并返回returncode属性。
  
Popen.poll()
  用于检查子进程是否已经结束。设置并返回returncode属性。
  
Popen.kill()
  杀死子进程。
 
Popen.stdin,Popen.stdout ,Popen.stderr ,官方文档上这么说:
stdin, stdout and stderr specify the executed programs’ standard input, standard output and standard error file handles, respectively. Valid values are PIPE, an existing file descriptor (a positive integer), an existing file object, and None.

Popen.pid
  获取子进程的进程ID。

Popen.returncode
  获取进程的返回值。如果进程还没有结束,返回None。
  
简单的用法:

import subprocess
p = subprocess.Popen("dir", shell=True)
p.wait()

shell参数根据你要执行的命令的情况来决定,上面是dir命令,就一定要shell=True了,p.wait()可以得到命令的返回值。
如果上面写成a=p.wait(),a就是returncode。那么输出a的话,有可能就是0【表示执行成功】。

进程通讯
如果想得到进程的输出,管道是个很方便的方法,这样:

import subprocess
p = subprocess.Popen("dir", shell=True, stdout=subprocess.PIPE, 		stderr=subprocess.PIPE)
(stdoutput, erroutput) = p.communicate()
print("communicate\n")
print(stdoutput[0:5], '    :    ', erroutput)

p.communicate会一直等到进程退出,并将标准输出和标准错误输出返回,这样就可以得到子进程的输出了。

如果你想一行行处理子进程的输出,也没有问题:

p=subprocess.Popen("dir", shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
while True:
    buff = p.stdout.readline()
    if buff == '' and p.poll() != None:
        break

死锁
但是如果你使用了管道,而又不去处理管道的输出,那么小心点,如果子进程输出数据过多,死锁就会发生了,比如下面的用法:

p=subprocess.Popen("longprint", shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
p.wait()

longprint是一个假想的有大量输出的进程,那么在我的xp, Python2.5的环境下,当输出达到4096时,死锁就发生了。当然,如果我们用p.stdout.readline或者p.communicate去清理输出,那么无论输出多少,死锁都是不会发生的。或者我们不使用管道,比如不做重定向,或者重定向到文件,也都是可以避免死锁的。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

python 中 subprocess.Popen 总结 的相关文章

随机推荐