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"

  

posted on 2016-08-28 13:11  妞溜溜  阅读(3525)  评论(0编辑  收藏  举报