脚本 1,负责输出
for i in range(1, 10):
print(i)
time.sleep(1)
脚本 2,用 subprocess 执行脚本 1,获取 stdout 并输出
import subprocess
proc = subprocess.Popen("python3 ./test1.py", shell=True, bufsize=1, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
while True:
line = proc.stdout.readline()
if not line:
break
print(line.rstrip())
期望的是实时输出,结果是一直等到脚本 1 执行完了才整体输出所有的内容
stackoverflow 上有一篇Non-blocking read on a subprocess.PIPE in python ,我试了线程和 fcntl,还是原样
本人小白,求大神~~