
正文
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:
passprint proc.poll()
print proc.stdout.read()

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








