python subprocess.Popen 非阻塞


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

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()!=0 :
#    if  proc.poll() is not None:
#        print proc.poll()
#    else:
#        pass 
#time.sleep(20)
non_block_read(proc.stdout)
print proc.stdout.read()
while proc.poll() is None:
    pass

print proc.poll()
print proc.stdout.read()

若子进程没有执行完 直接proc.stderr.read() 阻塞,若设置成非阻塞抛出如上异常

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM