默认情况下是如下代码,stdout和stderr是两个分别单独流,这也就导致了需要2个线程分别取处理,否则在一个线程里读取stdout时会进入阻塞模式,而恰巧stderr有输出,则无法捕获。
process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
将stdout和strderr合并成一个流,则解决方案:
process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
output = process.stdout.readline().decode()
if output == '' and process.poll() is not None:
break
if output:
print("=OUTPUT>>>",output)