import select
import os
import subprocess
import time
import fcntl args = ['python','./fetch_file2.py',ip,path]
proc = subprocess.Popen(args, stdout=subprocess.PIPE,stderr=subprocess.PIPE,close_fds=True) def non_block_read(output): # 避免阻塞
fd = output.fileno()
fl = fcntl.fcntl(fd, fcntl.F_GETFL)
fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK)
try:
return output.read()
except:
return "" while proc.poll() is None: #fetch中rsync结束。但是fetch没有结束(怀疑输出过大) 导致这里一直是None
pass print proc.poll() # 杀死fetch进程 返回-9
print proc.stderr.read() #阻塞
#方法1:
#non_block_read(proc.stderr) #防止阻塞
#方法2:
select_rfds = [ proc.stdout, proc.stderr]
(rfds, wfds, efds) = select.select(select_rfds, [],[])
if proc.stderr in rfds: #不存在。若select_rfds=[stderr],则阻塞在select上
len = proc.stderr.read(10)
if len == 0:
print "empty"
else:
print "proc.stderr" if proc.stdout in rfds:
print "proc.stdout"

  

python subprocess阻塞的更多相关文章

  1. Python之阻塞IO模型与非阻塞IO模型

    Python之阻塞IO模型与非阻塞IO模型 IO模型 1 阻塞IO: 全程阻塞 2 非阻塞IO: 发送多次系统调用: 优点:wait for data时无阻塞 缺点:1 系统调用太多 2 数据不是实时 ...

  2. 【python库模块】Python subprocess模块功能与常见用法实例详解

    前言 这篇文章主要介绍了Python subprocess模块功能与常见用法,结合实例形式详细分析了subprocess模块功能.常用函数相关使用技巧. 参考 1. Python subprocess ...

  3. python subprocess popen 静默模式(不弹出console控制台)

    python subprocess popen 静默模式(不弹出console控制台) import subprocess,sys IS_WIN32 = 'win32' in str(sys.plat ...

  4. python subprocess相关操作

    python subprocess常用操作 1.subprocess模块的常用函数 函数 描述 subprocess.run() Python 3.5中新增的函数.执行指定的命令,等待命令执行完成后返 ...

  5. 通过阅读python subprocess源码尝试实现非阻塞读取stdout以及非阻塞wait

    http://blog.chinaunix.net/uid-23504396-id-4661783.html 执行subprocess的时候,执行不是问题最麻烦的是获取进程执行后的回显来确认是否正确执 ...

  6. Python Subprocess Popen 管道阻塞问题分析解决

    http://ju.outofmemory.cn/entry/279026 场景:1>不断播放mp3文件: 2>使用订阅发布模式保持tcp长连接,从服务器接收信息 造成程序hang死,但是 ...

  7. python subprocess.Popen 非阻塞

    1.非阻塞设置subprocess.Popen(args, stdout=subprocess.PIPE,stderr=subprocess.PIPE) def non_block_read(outp ...

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

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

  9. Python subprocess模块学习总结

    从Python 2.4开始,Python引入subprocess模块来管理子进程,以取代一些旧模块的方法:如 os.system.os.spawn*.os.popen*.popen2.*.comman ...

随机推荐

  1. JSP - 9大内置对象及其API

    内置对象:不需要预先声明就可以在脚本代码和表达式中随意使用,有以下特点 1.由JSP规范提供,不用编写者实例化 2.提供Web容器实现和管理 3.所有JSP页面均可用 4.只有在脚本元素的表达式或者代 ...

  2. HTML 字符实体

    原文地址 在 HTML 中,某些字符是预留的. 在 HTML 中不能使用小于号(<)和大于号(>),这是因为浏览器会误认为它们是标签. 如果希望正确地显示预留字符,我们必须在 HTML 源 ...

  3. Atom编辑器在windows下怎么更改安装路径

    作为一个有良(mei)知(qian)的程序员,也不能老是用和谐版的source insight. 而且source insight也不是十分的完美,本身有一些缺陷. 比如说中文的支持,比如说反应很慢的 ...

  4. glusterfs 中的字典查询

    glusterfs文件系统是一个分布式的文件系统,但是与很多分布式文件系统不一样,它没有元数服务器,听说swift上也是应用了这个技术的.glusterfs中每个xlator的配置信息都是用dict进 ...

  5. Android之Viewpager+Fragment实现懒加载

    我们在做应用开发的时候,一个Activity里面可能会以viewpager(或其他容器)与多个Fragment来组合使用.而ViewPager默认会缓存三页数据,即:Viewpager每加载一个Fra ...

  6. java获取图片原始尺寸

    java获取图片原始尺寸 URL url = null; InputStream is = null; BufferedImage img = null; try { url = new URL(pi ...

  7. python学习笔记——2

    Python的字符串 在最新的Python 3版本中,字符串是以Unicode编码的,也就是说,Python的字符串支持多语言. 对于单个字符的编码,Python提供了ord()函数获取字符的整数表示 ...

  8. [系统开发] Python 实现的 Bind 智能 DNS Web 管理系统

    在公司的运营中,DNS还是很重要的,不仅名称解析需要DNS,一些重要的服务,比如负载均衡.HTTP 虚拟主机也会用到它.Bind 手工管理方式有一定的危险性,一旦写错格式就会造成 DNS 服务瘫痪. ...

  9. OAF_开发系列17_实现OAF数组应用Vector / Hashmap / Hashtable / Arraylist(案例)

    20150506 Created By BaoXinjian

  10. maven配置多模块项目事例

    limit-parent <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http:// ...