subprocess

文章目录

  • 常用内置方法
  • 1.subprocess.call()
  • 2.subprocess.check_call()
  • 3.subprocess.check_output()
  • 参数说明:
  • Popen 创建进程
  • Popen 常见内置对象
  • Popen 常见内置方法

常用内置方法

1.subprocess.call()

subprocess.call(
	args,*,
	stdin=None,
	stdout=None,
	stderr=None,
	shell=False
	)
  • 执行args指定的命令,直到命令结束,返回返回码的属性值
  • shell=True(安全保护机制)。此时不要使用stdout=PIPEstderr=PIPI,不然会导致进程输出的死锁
  • 如要用管道,可在communicate()方法中使用Popen

2.subprocess.check_call()

subprocess.check_call(
	args,*,
	stdin=None,
	stdout=None,
	stderr=None,
	shell=False)
  • 执行args指定的命令,直到命令结束
  • 返回码为0则返回,否则抛出CalledProcessError异常
  • shell=True(安全保护机制)。
    此时不要使用stdout=PIPEstderr=PIPI,不然会导致进程输出的死锁
  • 如要用管道,可在communicate()方法中使用Popen

3.subprocess.check_output()

subprocess.check_output(
	args,*,
	stdin=None,
	stderr=None,
	shell=False,
	univeral_newlines=False)
  • 执行args指定的命令

  • 返回码为0则返回,否则抛出CalledProcessError异常
  • shell=True(安全保护机制)。此时不要使用stdout=PIPEstderr=PIPI,不然会导致进程输出的死锁
  • 如要用管道,可在communicate()方法中使用Popen

参数说明:

  • args
  • 字符串 或 程序参数序列(推荐)
  • stdin, stdout, stderr
  • 标准输入、输出、错误的文件句柄
  • 可以是 PIPENone (默认)
  • shell
  • True 则通过命令行来执行
improt subprocess
subprocess.call(['python', 'test.py'])

  上面的几个函数都是基于Popen()的封装(wrapper)。这些封装的目的在于让我们容易使用子进程。

  当我们想要更个性化我们的需求的时候,就要转向Popen类,该类生成的对象用来代表子进程.

Popen 创建进程

Popen对象创建后,主程序不会自动等待子进程完成。

我们必须调用对象的wait()方法,父进程才会等待 (也就是阻塞block)

subprocess.Popen(args,
	bufsize=-1,
	excutable=None,
	stdin=None,stdou=None,stderr=None,
	preexec_fn=None,
	close_fds=True,
	shell=False,
	cwd=None,env=None,
	universal_newlines=False,
	startupinfo=None,
	creationflags=0
	)
  • args
  • 字符串或序列类型(列表、元组)
  • 指定进程的可执行文件及其参数
  • excutable
  • 指定可执行程序
  • shell=True:用于指定程序使用的 shell
  • bufsize
  • 指定缓冲区大小
  • stdin, stdout, stderr
  • 表示程序标准输入、输出、错误句柄。
  • 可为PIPE、文件描述符、文件对象、None
  • preexec_fn
  • 仅在UNIX平台有效。
  • 指定一 可调用对象,它将在子进程之前调用
  • close_fds
  • Windows:True,新建子进程将不会继承父进程的输入、输出、错误管道
  • 不能同时True 和 同时定向子进程的stdin、stdout、stderr
  • shell
  • True:通过shell执行
  • cwd
  • 设置子进程的当前目录
  • env
  • 字典类型
  • 用于指定子进程环境变量
  • None:子进程环境变量将从父进程继承
  • universal_newlines
  • True:将换行符当做\n处理
  • startupinfocreationfalgs
  • 只在Windows有效。
  • 用于设置子进程一些属性

Popen 常见内置对象

  • subprocess.PIPE
  • 创建Popen对象时,PIPE可以初始化stdin、stdout、stderr
  • 表示与子进程通信的标准流
  • subprocess.SRDOUT
  • 创建Popen对象时,用于初始化stderr参数,表示将错误通过标准输出流输出

Popen 常见内置方法

  • poll() 检查子进程是否结束。设置并返回returncode属性
  • wait() 等待子进程结束。设置并返回returncode属性
  • communicate(input=None)
  • 与子进程交互。向stdin发送数据、从stdout/stderr读取数据
  • input:发送到子进程的参数
  • 需要子进程stdin向其发送数据,创建Popenstdin设置为PIPE
  • 需要从stdoutstderr获取数据,创建时…设置为PIPE
  • send_signal(signal) 向子进程发送信号
  • terminate() 停止(stop)子进程。
  • kill() 终止子进程
  • Popen.stdin
  • 创建时stdin如为PIPE,返回一文件对象,用于向子进程发送指令
  • 否则,返回None
  • Popen.stdout
  • 创建时stdout如为PIPE,返回一文件对象,用于向子进程发送指令
  • 否则,返回None
  • Popen.stderr
  • 创建时stderr如为PIPE,返回一文件对象,用于向子进程发送指令
  • 否则,返回None
  • Popen.pid 获取子进程的进程ID
  • Popen.returncode 获取进程返回值
    如果进程还没结束,返回None
import subprocess

prcs = subprocess.Popen(['Python', 'test.py'],
	stdout=subprocess.PIPE,
	stdin=subprocess.PIPE,
	stderr=subprocess.PIPE,
	universal_newlines=True,
	shell=True)
prcs.communicate("这些文字都来自: stdin.")	
print('subprocess pid: ', prcs.pid)
print('\nSTDOUT:')
print(str(prcs.communicate()[0]))
print('\nSTDERR:')
print(prcs.communicate()[1])