连续捕获subprocess.Popen的输出

时间:2017-08-29 13:05:27

标签: python python-3.x dictionary subprocess

我想捕获使用

调用的进程的输出
cmd = "doit input={input} conf={conf} output_dir={dir} gpu={gpu} --db-file .doit_gpu{gpu_id}.db".format(**kwargs)
process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)

连续。 此过程调用在控制台中将其结果作为字典字符串打印的函数。 E.g:

{'subprocess_1': [<some data>]}
# then after some time
{'subprocess_2': [<some data>]}
# and some seconds later
{'subprocess_3': [<some data>]}

我希望能够连续捕获输出并将其立即重定向到我之前初始化的另一个字典。

process_table = {}

所以这就是整个过程结束后字典的样子:

process_table = {
    {'subprocess_1': [<some data>]},
    {'subprocess_2': [<some data>]},
    {'subprocess_3': [<some data>]}
}

这里的关键点(这就是我似乎没有找到任何相关问题的原因)是process_table应该在子流程完成后立即更新,而不仅仅是在整个流程完成之后。这就是为什么这个答案here 还不够

for line in process.stdout:
    print("this is the output:", line,)

1 个答案:

答案 0 :(得分:0)

I have found the solution to my problem. A simple

sys.stdout.flush()

in the executed program (i.e. my doit script) printed the subprocess outputs immediately and I could save them to the process_table with

for line in process.stdout:
    subprocess_output = json.loads(line.decode("utf-8"))
    process_table.update(subprocess_output)