import sys
from subprocess import PIPE, Popen
from threading import Thread try:
from Queue import Queue, Empty
except ImportError:
from queue import Queue, Empty # python 3.x ON_POSIX = 'posix' in sys.builtin_module_names def enqueue_output(out, queue):
for line in iter(out.readline, b''):
queue.put(line)
out.close() p = Popen(['myprogram.exe'], stdout=PIPE, bufsize=1, close_fds=ON_POSIX)
q = Queue()
t = Thread(target=enqueue_output, args=(p.stdout, q))
t.daemon = True # thread dies with the program
t.start() # ... do other things here # read line without blocking
try: line = q.get_nowait() # or q.get(timeout=.1)
except Empty:
print('no output yet')
else: # got line
# ... do something with line

  http://stackoverflow.com/questions/375427/non-blocking-read-on-a-subprocess-pipe-in-python

Non-blocking read on a subprocess.PIPE in python的更多相关文章

  1. python subprocess pipe 实时输出日志

    * test11.py import time print "1" time.sleep(2) print "1" time.sleep(2) print &q ...

  2. 记录一次因subprocess PIPE 引起的线上故障

    sence:python中使用subprocess.Popen(cmd, stdout=sys.STDOUT, stderr=sys.STDERR, shell=True) ,stdout, stde ...

  3. Python 第五篇(下):系统标准模块(shutil、logging、shelve、configparser、subprocess、xml、yaml、自定义模块)

    目录: shutil logging模块 shelve configparser subprocess xml处理 yaml处理 自定义模块 一,系统标准模块: 1.shutil:是一种高层次的文件操 ...

  4. python标准模块(下)

    Python 系统标准模块(shutil.logging.shelve.configparser.subprocess.xml.yaml.自定义模块) 目录: shutil logging模块 she ...

  5. Python 3 利用 subprocess 实现管道( pipe )交互操作读/写通信

    这里我们用Windows下的shell来举例: from subprocess import * #因为是举例,就全部导入了 为了方便你理解,我们用一个很简单的一段代码来说明: 可以看见我们利用Pop ...

  6. Python第十一天 异常处理 glob模块和shlex模块 打开外部程序和subprocess模块 subprocess类 Pipe管道 operator模块 sorted函数 os模块 hashlib模块 platform模块 csv模块

    Python第十一天    异常处理  glob模块和shlex模块    打开外部程序和subprocess模块  subprocess类  Pipe管道  operator模块   sorted函 ...

  7. Python subprocess.Popen communicate() 和wait()使用上的区别

    之所以会纠结到这个问题上是因为发现在调用Popen的wait方法之后程序一直没有返回.google发现wait是有可能产生死锁的.为了把这个问题彻底弄清楚,搜索一些资料过来看看: 原文链接:http: ...

  8. python学习道路(day7note)(subprocess模块,面向对象)

    1.subprocess模块   因为方法较多我就写在code里面了,后面有注释 #!/usr/bin/env python #_*_coding:utf-8_*_ #linux 上调用python脚 ...

  9. >Python下使用subprocess中文乱码的解决方案

    # -*- coding: CP936 -*- import subprocess cmd="cmd.exe" begin=101 end=110 while begin<e ...

随机推荐

  1. [deviceone开发]-心形点赞动画示例

    一.简介 这个示例展示do_Animator组件的简单使用,通过点击"点赞"按钮,不断弹出心形图片,向上动画漂移到顶部消失.间隔时间和上下左右移动的步长都是一定范围的随机值.二.效 ...

  2. 负载均衡之LVS集群

    h3 { color: rgb(255, 255, 255); background-color: rgb(30,144,255); padding: 3px; margin: 10px 0px } ...

  3. Jquery更改table中的内容(一)

    css部分: .tab{ border:solid 1px #00aaee; text-align: left; margin:20px;}.tab tr{ border-top: solid 1px ...

  4. 利用BI搭建零售业数据信息平台

    某百货公司是全市规模最大的以零售为主.多元化经营的股份制商业企业.拥有员工数千人,经营国内外品牌2300余种,年商品销售额逾10亿人元. 销售体量如此庞大的企业近几年在IT建设上出现了问题,集团内部的 ...

  5. Vagrant基础简要记录

    Vagrant是一种开源软件,它为跨众多操作系统构建可重复的开发环境提供了一种方法.Vagrant使用提供者(provider)来启动隔离的虚拟环境.默认的提供者是Virtualbox Vagrant ...

  6. scrollToItemAtIndexPath: atScrollPosition: animated:

    oh my god 今天死在scrollToItemAtIndexPath: atScrollPosition: animated:方法上面,scrollPosition这个参数控制cell具体停留在 ...

  7. IOS开发基础知识碎片-导航

    1:IOS开发基础知识--碎片1 a:NSString与NSInteger的互换 b:Objective-c中集合里面不能存放基础类型,比如int string float等,只能把它们转化成对象才可 ...

  8. 转载文章(Redis中对key的操作)

    转载地址:http://www.cnblogs.com/stephen-liu74/archive/2012/03/26/2356951.html 一.概述: 在该系列的前几篇博客中,主要讲述的是与R ...

  9. 安装phpstudy之后发现80端口被占用

    安装phpstudy之后发现80端口被占用: 进入cmd的界面,在输入"netstat -ano",按回车键后,会显示当前电脑中程序占用的端口和程序ID等等信息:看到第一条就是:0 ...

  10. Linux paste命令

    Linux paste命令用于合并文件的列. paste指令会把每个文件以列对列的方式,一列列地加以合并. 语法 paste [-s][-d <间隔字符>][--help][--versi ...