我的文件结构是这样的:
runner.py
scripts/
something_a/
main.py
other_file.py
something_b/
main.py
anythingelse.py
something_c/
main.py
...
runner.py 应该查看 scripts/ 中的所有文件夹以运行位于那里的 main.py。
现在我正在通过 subprocess.check_output 实现这一点。它可以工作,但是其中一些脚本需要很长时间才能运行,而且我看不到任何进展;它在过程完成后打印所有内容。
我希望找到一种解决方案,可以轻松完成两件事:
1) Stream the output instead of getting it all at the end
2) Doesn't prohibit running multiple scripts at once
这可能吗?我见过的很多从另一个运行 Python 脚本的解决方案都需要知道另一个脚本的名称/位置。如果有帮助,我可以还强制所有 main.py 具有特定功能。
请您参考如下方法:
您可以使用 Popen 遍历每个文件并将其内容写入多个日志文件。然后,您可以实时读取这些文件,同时填充每个文件。 :)
由于可读性,您希望如何将输出转换为更易读的格式有点棘手。您可以使用 Popen 创建另一个脚本来读取这些日志文件,并决定您希望如何以可理解的方式回读这些信息。
""" Use the same command as you would do for check_output """
cmd = ''
for filename in scriptList:
log = filename + ".log"
with io.open(filename, mode=log) as out:
subprocess.Popen(cmd, stdout=out, stderr=out)
