import subprocess as subp
import time
import os
# run program in background
pid = int(subp.check_output("python3 test.py >test.out 2>&1 & echo $!",
shell=True))
print("pid", pid)
# monitor output file
pos = 0
done = False
while not done:
time.sleep(.1)
if os.stat('test.out').st_size > pos:
with open('test.out', 'rb') as fp:
fp.seek(pos)
for line in fp.readlines():
print(line.strip().decode())
done = b'done' in line
pos = fp.tell()
print("test complete")
check_output
等待输出管道关闭,sleep
也有输出管道。您可以重定向到/dev/null
立即返回。在更新
很难说
sleep 1
是否真的在后台运行,所以我写了一个稍微大一点的测试。在测试.py-将时间写入
^{pr2}$stdout
5秒跑步者.py-运行重定向到文件的测试并监视该文件。在
运行它,我明白了
相关问题 更多 >
编程相关推荐