python - 标准库:subprocess模块
subprocess的目的就是启动一个新的进程并且与之通信。
subprocess模块中只定义了一个类: 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, stdout或stderr参数。表示与子进程通信的标准流。
subprocess.STDOUT:创建Popen对象时,用于初始化stderr参数,表示将错误通过标准输出流输出
==========================================================================================================================
Popen的方法:
Popen.poll()
用于检查子进程是否已经结束。设置并返回returncode属性。
Popen.wait()
等待子进程结束。设置并返回returncode属性。
Popen.communicate(input=None)
与子进程进行交互。向stdin发送数据,或从stdout和stderr中读取数据。可选参数input指定发送到子进程的参数。Communicate()返回一个元组:(stdoutdata, stderrdata)。注意:如果希望通过进程的stdin向其发送数据,在创建Popen对象的时候,参数stdin必须被设置为PIPE。同样,如果希望从stdout和stderr获取数据,必须将stdout和stderr设置为PIPE。
Popen.send_signal(signal)
向子进程发送信号。
Popen.terminate()
停止(stop)子进程。在windows平台下,该方法将调用Windows API TerminateProcess()来结束子进程。
Popen.kill():杀死子进程。
==========================================================================================================================
简单应用:
p=subprocess.Popen("dir", shell=True)
p.wait()
shell参数根据你要执行的命令的情况来决定,上面是dir命令,就一定要shell=True了,p.wait()可以得到命令的返回值。
如果上面写成a=p.wait(),a就是returncode。那么输出a的话,有可能就是0【表示执行成功】。
p=subprocess.Popen("dir", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(stdoutput,erroutput) = p.communicate()
p.communicate会一直等到进程退出,并将标准输出和标准错误输出返回,这样就可以得到子进程的输出了。
==========================================================================================================================
代码分析:
......
self.root_env = {'USER': 'root', 'HOME': '/root'} def loca_cmd(self, cmd, env):
p = subprocess.Popen(cmd,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
env=env,
shell=True #shell=true ,程序将通过shell执行)
stdout,stderr = p.communicate()
return stdout #print stdout[0] => 'a' def collect_node_info(self):
check_yml_cmd = "cat qmonitor_cluster.yml | grep ^[^#] "
stdout = self.loca_cmd(check_yml_cmd, self.root_env)
if stdout == "":
self.qmonitor_node_info = {'api': '127.0.0.1', 'schedule': '127.0.0.1', 'mysql': [{'master': '127.0.0.1'}], 'qmonitor_master': '127.0.0.1', 'node_ip': [{'ip': '127.0.0.1', 'perf_num': 4}], 'jetty': '127.0.0.1'}
self.qmonitor_node_list = [{'ip': '127.0.0.1', 'perf_num': 4}]
else:
try:
import paramiko
import yaml
current_path = os.path.dirname(os.path.realpath(__file__))
qmonitor_cluster = os.path.join(
current_path, "qmonitor_cluster.yml")
with open(qmonitor_cluster) as f:
text = f.read()
config_dict = yaml.load(text)
self.qmonitor_node_info = config_dict["qmonitor_cluster"]
self.qmonitor_node_list = self.qmonitor_node_info["node_ip"]
except Exception, e:
self.print_sys_error(
"please config yum repo and execute : yum install -y PyYAML python-paramiko")
sys.exit(0)
=====================================================================================================================================
死锁
如果你使用了管道,而又不去处理管道的输出,那么小心点,如果子进程输出数据过多,死锁就会发生了,比如下面的用法:
p=subprocess.Popen("longprint", shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
p.wait()
longprint是一个假想的有大量输出的进程,那么在我的xp, Python2.5的环境下,当输出达到4096时,死锁就发生了。当然,如果我们用p.stdout.readline或者p.communicate去清理输出,那么无论输出多少,死锁都是不会发生的。或者我们不使用管道,比如不做重定向,或者重定向到文件,也都是可以避免死锁的。
python - 标准库:subprocess模块的更多相关文章
- [python标准库]Pickle模块
Pickle-------python对象序列化 本文主要阐述以下几点: 1.pickle模块简介 2.pickle模块提供的方法 3.注意事项 4.实例解析 1.pickle模块简介 The pic ...
- Python 标准库 ConfigParser 模块 的使用
Python 标准库 ConfigParser 模块 的使用 demo #!/usr/bin/env python # coding=utf-8 import ConfigParser import ...
- Python标准库——collections模块的Counter类
1.collections模块 collections模块自Python 2.4版本开始被引入,包含了dict.set.list.tuple以外的一些特殊的容器类型,分别是: OrderedDict类 ...
- [python标准库]XML模块
1.什么是XML XML是可扩展标记语言(Extensible Markup Language)的缩写,其中的 标记(markup)是关键部分.您可以创建内容,然后使用限定标记标记它,从而使每个单词. ...
- 【python】Python标准库defaultdict模块
来源:http://www.ynpxrz.com/n1031711c2023.aspx Python标准库中collections对集合类型的数据结构进行了很多拓展操作,这些操作在我们使用集合的时候会 ...
- python 标准库 -- subprocess
subprocess 主要功能室执行外部的命令和程序 一个进程可 fork 一个子进程, 并让这个子进程 exec 另外一个程序. 在 python 中, 可以通过标准库中的 subprocess 包 ...
- Python标准库--os模块
这个模块包含普遍的操作系统功能.如果你希望你的程序能够与平台无关的话,这个模块是尤为重要的.即它允许一个程序在编写后不需要任何改动,也不会发生任何问题,就可以在Linux和Windows下运行.一个例 ...
- python标准库 bisect模块
# -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' #bisect #作用:维护有序列表,而不必在每次向列表增加一个元素 ...
- python标准库 sysconfig模块
# -*- coding: utf-8 -*-# python:2.x__author__ = 'Administrator'import sysconfig#sysconfig:解释器编译时配置#作 ...
- Python标准库 -- UUID模块(生成唯一标识)
UUID是什么: UUID: 通用唯一标识符 ( Universally Unique Identifier ),对于所有的UUID它可以保证在空间和时间上的唯一性,也称为GUID,全称为: UUID ...
随机推荐
- 运维日常错误总结(docker)
一:Apache服务启动失败 报错原因:80端口被占用 分析:netstat -anp|grep 80 检查80端口的占用情况,发现是启动了nginx服务,占用了http服务 解决方式: 1:如ngi ...
- 交叉工具链和makefile
交叉工具链: arm-linux-gcc:交叉编译器 arm-linux-ld:交叉连接器 arm-linux-readelf:交叉ELF文件工具 arm-linux-objdump:交叉反汇编器 a ...
- keepalived的工作原理
keepalived的工作原理 首先简单介绍一下vrrp协议 vrrp协议 用来实现路由器冗余的协议: Vrrp协议是为了消除在静态缺省路由环境下路由器单点故障引起的网络失效而设计的主备模式的协议,使 ...
- GetShortPathName函数
Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathName" (ByVal ...
- zencart批量表上传后 标题显示为网址 批量修改标题状态 SEO三要素
zencart批量表上传后 标题显示为网址,原因是导入批量表时,产品标题对应状态被重置为0导致的 批量修改标题状态 ', metatags_products_name_status ', metata ...
- mysql远程不能连接问题
解决Navicat无法连接到腾讯云MySQL的问题 工具/原料 Navicat phpstudy 腾讯云 Xshell6 方法/步骤 1 1.首先远程连接进入服务器,在cmd中输入mysql ...
- centos7 远程桌面连接到xfce桌面
1 安装xfce $ sudo yum install -y epel-release $ sudo yum groupinstall -y "Xfce" $ sudo reboo ...
- git 合并某个分支指定的文件
$ git checkout <another-branch> <path-to-file> [<one-more-file> ...] $ git status ...
- 美国的电信巨头T-Mobile今天披露了另一起数据遭黑客泄露事件
您是T-Mobile预付费客户吗?如果是,您应该立即创建或更新您关联的帐户PIN /密码,以提供额外的保护.总部位于美国的电信巨头T-Mobile今天披露了另一起数据泄露事件,该事件最近暴露了一些使用 ...
- 微信点餐系统(七)-微信授权获取openid:
章节小结: 1.学会了微信授权的步骤,学会了微信授权的文档 2.学会了使用natapp内网穿透工具 3.加深了虚拟机的网络配置以及基本使用 4.学会了抓包购票工具fiddler的使用 5.微信授权步骤 ...