python subprocess阻塞
2016-08-28 13:11
323 查看
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.Popen 非阻塞
- Python Subprocess Popen 管道阻塞问题分析解决
- Python Subprocess Popen 管道阻塞问题分析解决
- Python Subprocess Popen 管道阻塞问题分析解决
- Python_cmd的各种实现方法及优劣(subprocess.Popen, os.system和commands.getstatusoutput)
- python 使用 subprocess.Popen() 调用子进程
- PYTHON SUBPROCESS
- Python IDLE 运行错误:IDLE's subprocess didn't make connection 的解决方法
- Python subprocess shell 编程规范
- Python subprocess 创建子进程
- Python subprocess Popen 的一点说明
- 关于python sunprocess调用wait后,一直阻塞的问题
- python类库31[进程subprocess与管道pipe]
- python 启动提示IDLE's subprocess didn't make conne...
- python subprocess
- [Python] subprocess module
- Python subprocess 模块, 替换os.system
- Python使用subprocess.Popen导致子进程hang住
- Python subprocess shell 丢失环境变量
- Python_cmd的各种实现方法及优劣(subprocess.Popen, os.system和commands.getstatusoutput)