subprocess实时获取结果和捕获错误
需要调用命令行来执行某些命令,主要是用 subprocess
实时获取结果和捕获错误,发现subprocess的很多坑。
subprocess
普通获取结果方式,其需要命令完全执行才能返回结果:
import subprocess
scheduler_order = "df -h"
return_info = subprocess.Popen(scheduler_order, shell=True, stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
for next_line in return_info.stdout:
return_line = next_line.decode("utf-8", "ignore")
print(return_line)
客subprocess
实时获取结果:
import subprocess
scheduler_order = "df -h"
return_info = subprocess.Popen(scheduler_order, shell=True, stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
while True:
next_line = return_info.stdout.readline()
return_line = next_line.decode("utf-8", "ignore")
if return_line == '' and return_info.poll() != None:
break
print(return_line)
想要获取报错机制,使用 check_output 捕捉报错和使用 check_call 捕捉报错,及时在 Popen 中捕获报错,都会使 实时输出失效 !,所以自行查看 CalledProcessError
源码终于搞定。
实时发送以及捕获报错:
import subprocess
try:
scheduler_order = "top -u ybtao"
return_info = subprocess.Popen(scheduler_order, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
while True:
next_line = return_info.stdout.readline()
return_line = next_line.decode("utf-8", "ignore")
if return_line == '' and return_info.poll() != None:
break
print(return_line)
returncode = return_info.wait()
if returncode:
raise subprocess.CalledProcessError(returncode, return_info)
except Exception as e:
print(e)
subprocess实时获取结果和捕获错误的更多相关文章
- subprocess.Popen stdout重定向内容实时获取
python 打开一个新进程执行系统命令, test 执行完才能获取返回, test1 实时获取返回结果 import subprocess def test(cmd): p = subprocess ...
- 用angular实时获取本地localStorage数据,实现一个模拟后台数据登入的效果
研究了一上午,终于做出了,实时获取本地localStorage来模拟注册登入~~~ <!DOCTYPE html><html><head lang="en&qu ...
- 使用getCurrentPosition方法实时获取当前Geolocation信息(赋源码文件)--html5、JavaScript
使用getCurrentPosition方法实时获取当前Geolocation信息: 1.typeof 运算符返回一个用来表示表达式的数据类型的字符串 <script> alert(typ ...
- 使用getCurrentPosition方法实时获取当前Geolocation信息(附源码文件)--html5、JavaScript
使用getCurrentPosition方法实时获取当前Geolocation信息: 1.getCurrentPosition方法的使用 navigator.geolocation.getCurren ...
- jquery的实时触发事件(textarea实时获取中文个数)
jquery的实时触发事件(textarea实时获取中文个数) (2014-09-16 11:49:50) 转载▼ 标签: 实时触发事件 中文个数 onpropertychange oninput o ...
- geotrellis使用(二十二)实时获取点状目标对应的栅格数据值
目录 前言 实现方法 总结 一.前言 其实这个功能之前已经实现,今天将其采用1.0版的方式进行了重构与完善,现将该内容进行总结. 其实这个功能很常见,比如google地球上 ...
- Android网络开发之实时获取最新数据
在实际开发中更多的是需要我们实时获取最新数据,比如道路流量.实时天气信息等,这时就需要通过一个线程来控制视图的更新. 示例:我们首先创建一个网页来显示系统当前的时间,然后在Android程序中每隔5秒 ...
- file_get_contents()获取https出现这个错误Unable to find the wrapper “https” – did
file_get_contents()获取https出现这个错误Unable to find the wrapper “https” – did 解决办法 ,如果你是用的服务器,可以参考这个办法,修改 ...
- Yii2 捕获错误日志
在技术开发中,捕获程序框架错误,是非常必要的一件事情,我们公司使用Yii2框架,简单说下Yii2的错误捕获处理 Yii2 web应用 1 配置如下 其中errorHandler就是错误处理配置,执行E ...
随机推荐
- Apriori算法思想和其python实现
第十一章 使用Apriori算法进行关联分析 一.导语 "啤酒和尿布"问题属于经典的关联分析.在零售业,医药业等我们经常需要是要关联分析.我们之所以要使用关联分析,其目的是为了从大 ...
- 深入理解javascript 匿名函数和闭包
代码如下: (function(){ //这里忽略jQuery所有实现 })(); (function(){ //这里忽略jQuery所有实现 })(); 半年前初次接触jQuery的时候,我也像其 ...
- maven包加载
1) IDEA包加载pom.xml配置 <build> <sourceDirectory>src/main/java</sourceDirectory> ...
- windows下安装mysql-5.7.11-winx64
1.解压. 2.将『D:\Program Files\mysql-5.7.11-winx64\bin』加入系统环境变量. 3.修改my-default.ini. 4.初始化data目录,在 ...
- 对Python这门课程的理解。
这门课程是现在热门,对之后的就业和利用的帮助还是很大的. 希望能学完整本书并且能学以致用,而不是单单只获得理论知识. 学完之后能用于数据库.大数据处理.图形编程等等
- 如何更好的编写async函数
2018年已经到了5月份,node的4.x版本也已经停止了维护 我司的某个服务也已经切到了8.x,目前正在做koa2.x的迁移 将之前的generator全部替换为async 但是,在替换的过程中,发 ...
- Axios源码深度剖析 - 替代$.ajax,成为xhr的新霸主
前戏 在正式开始axios讲解前,让我们先想想,如何对现有的$.ajax进行简单的封装,就可以直接使用原声Promise了? let axios = function(config){ return ...
- RocketMQ部分数据消费不了问题排查
问题现象 今天忽然收到RocketMQ预警信息如下: 提醒有部分数据没有消费,产生堆积情况. 打开RocketMq-Console-Ng查看如下图形式: 备注:第一反应是Consumer Group内 ...
- Tomcat配置与优化(内存、并发、管理)与性能监控
原文链接:http://blog.csdn.net/xyang81/article/details/51530979 一.JVM内存配置优化 在开发当中,当一个项目比较大时,依赖的jar包通常比较多, ...
- mysql由于权限问题看不到用户数据库
一.登录数据库 [root@localhost ~]# mysql -u root mysql > show databases; +--------------------+| Databas ...