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() 阻塞,若设置成非阻塞抛出如上异常