I want to capture the output of a process called with
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)
continuously. This process calls functions that print their result in the console as dictionary strings. E.g.:
{'subprocess_1': [<some data>]}
# then after some time
{'subprocess_2': [<some data>]}
# and some seconds later
{'subprocess_3': [<some data>]}
I want to be able to continuously capture the output and redirect it immediately to another dictionary that I initialized before.
process_table = {}
So this is how the dictionary would have to look like after the whole process finished:
process_table = {
{'subprocess_1': [<some data>]},
{'subprocess_2': [<some data>]},
{'subprocess_3': [<some data>]}
}
The key point here (which is why I don't seem to find any related questions) is that process_table
should be updated immediately after a subprocess finishes, not only after the entire process finished. This is why this answer here is not enough:
for line in process.stdout:
print("this is the output:", line,)