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