python 打开一个新进程执行系统命令, test 执行完才能获取返回, test1 实时获取返回结果

import subprocess

def test(cmd):
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
lines = p.stdout.readlines()
for line in lines:
tmp = line.decode('gbk').strip()
print(tmp) def test1(cmd):
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
for i in iter(p.stdout.readline, ''):
if len(i) < 1:
break
print(i.decode('gbk').strip()) if __name__ == '__main__':
test("ping www.baidu.com")
test1("ping www.baidu.com")

subprocess.Popen stdout重定向内容实时获取的更多相关文章

  1. python中subprocess.Popen执行命令并持续获取返回值

    先举一个Android查询连接设备的命令来看看Python中subprocess.Popen怎么样的写法.用到的命令为 adb devices. import subprocess order='ad ...

  2. subprocess实时获取结果和捕获错误

    需要调用命令行来执行某些命令,主要是用 subprocess 实时获取结果和捕获错误,发现subprocess的很多坑. subprocess 普通获取结果方式,其需要命令完全执行才能返回结果: im ...

  3. python subprocess.Popen 控制台输出 实时监控百度网ping值

    import subprocess file_out = subprocess.Popen('ping www.baidu.com', shell=True, stdout=subprocess.PI ...

  4. (很难啊)如何实时获取DBGrid 中当前单元格输入的内容? [问题点数:100分,结帖人yifawu100]

    如何获取DBGrid 中当前单元格输入的内容? 还没输入完成,我想实时获取 Cell中的内容,以便作其他处理,用什么事件呢? 所以Field的Onchange事件是没用的. DBGrid1.Selec ...

  5. 如何实时获取DBGrid 中当前单元格输入的内容?

    如何获取DBGrid 中当前单元格输入的内容? 还没输入完成,我想实时获取 Cell中的内容,以便作其他处理, 用什么事件呢? 所以Field的Onchange事件是没用的. 这个问题简单啊,每输入1 ...

  6. Python调用subprocess.Popen卡死的解决方案

    转载自:https://www.cnblogs.com/keke-xiaoxiami/p/7875009.html 在Python中,调用:subprocess.Popen(cmd, stdout = ...

  7. subprocess.Popen模块

    该类用于在一个新的进程中执行一个子程序.subprocess模块底层的进程创建和管理是由Popen类来处理的. 1.subprocess.Popen的构造函数 class subprocess.Pop ...

  8. 【python中调用shell命令使用PIPE】使用PIPE作为stdout出现假卡死的情况——将stdout重定向为输出到临时文件

    在Python中,调用:subprocess.Popen(cmd, stdout = PIPE, stderr = PIPE, shell= true)的时候,如果调用的shell命令本身在执行之后会 ...

  9. 使用Python实时获取cmd的输出

    最近发现一个问题,一个小伙儿写的console程序不够健壮,监听SOCKET的时候容易崩,造成程序的整体奔溃,无奈他没有找到问题的解决办法,一直解决不了,可是这又是一个监控程序,还是比较要紧的,又必须 ...

随机推荐

  1. Vulkan Device Memory

    1.通过下面的接口,可以获得显卡支持的所有内存类型: MemoryType的类型如下: 2.引用索引3对内存的描述 我们可以通过调用vkGetPhysicalDeviceMemoryPropertie ...

  2. jQuery省市联动(XML/JSON)

    准备: 导包 在src下导入c3p0-config.xml 导入JDBCUtil 创建数据库 新建js文件夹导入jQuery配置文件 NO01:创建city.jsp页面 <%@ page lan ...

  3. 文献阅读报告 - Social Ways: Learning Multi-Modal Distributions of Pedestrian Trajectories with GANs

    文献引用 Amirian J, Hayet J B, Pettre J. Social Ways: Learning Multi-Modal Distributions of Pedestrian T ...

  4. (20)sopel算法

    基础知识的理论,主要看这个博客:https://blog.csdn.net/github_38140310/article/details/68959931 然后代码展示: #include &quo ...

  5. Elasticsearch 更新文档

    章节 Elasticsearch 基本概念 Elasticsearch 安装 Elasticsearch 使用集群 Elasticsearch 健康检查 Elasticsearch 列出索引 Elas ...

  6. 【转载】WebDriver拾级而上·之零 WebDriver理论

    Selenium2.0 = Selenium1.0 + WebDriver(也就是说Selenium2.0合并了这两个项目)   Selenium1.0可以使用任何编程语言,但是有个先决条件就是必须支 ...

  7. Inheritance and the prototype chain 继承和 原型 链

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_chain Inherita ...

  8. (排序EX)P1093 奖学金

    题解: #include<iostream>using namespace std;int r=0;void swap(int &a,int &b){  int t=a;  ...

  9. prometheus配置简介

    参考网页:https://my.oschina.net/wangyunlong/blog/3060776 global: scrape_interval:             15s evalua ...

  10. jq监听

    $(window).resize(function () {//风电月表格自适应高度 var handHeight =$(".tree-handler").height() $(& ...