I'm trying to use subprocess.Popen to run a command, and I've chosen this over subprocess.run, as I need the output from the command printed to the shell.
I'm calling the commands in a for loop, and modifying the environment for each iteration, but python only runs the first command, and not the other 6.
I'm pretty sure this is a bug as I can't find anything else like this, but I thought I'd check before I submit the "bug".
The relevant code looks like this
input_file = Path("input.json").open()
for thread in threads:
new_env = default_env
new_env["OMP_NUM_THREADS"] = str(thread)
print("Starting run for {} threads".format(thread))
process = subprocess.Popen(
benchmark_command, env=new_env, stdin=input_file, stdout=subprocess.PIPE)
lines = []
for line in process.stdout:
print(line.decode(), end='')
results.append(Result(lines, thread))
print("Completed run for {} threads".format(thread))
and the current output is this
❯ python python/plot_results.py
Starting run for 1 threads
<expected output from running command>
Completed run for 1 threads
Starting run for 2 threads
Completed run for 2 threads
Starting run for 4 threads
Completed run for 4 threads
Starting run for 8 threads
Completed run for 8 threads
Starting run for 16 threads
Completed run for 16 threads
Starting run for 32 threads
Completed run for 32 threads
Starting run for 56 threads
Completed run for 56 threads
but should look like this
❯ python python/plot_results.py
Starting run for 1 threads
<expected output from running command>
Completed run for 1 threads
Starting run for 2 threads
<expected output from running command>
Completed run for 2 threads
Starting run for 4 threads
<expected output from running command>
Completed run for 4 threads
Starting run for 8 threads
<expected output from running command>
Completed run for 8 threads
Starting run for 16 threads
<expected output from running command>
Completed run for 16 threads
Starting run for 32 threads
<expected output from running command>
Completed run for 32 threads
Starting run for 56 threads
<expected output from running command>
Completed run for 56 threads
Per @jasonharper's suggestion, I needed to make a new open file object for each iteration.
input_file = Path("input.json")
for thread in threads:
new_env = default_env
new_env["OMP_NUM_THREADS"] = str(thread)
print("Starting run for {} threads".format(thread))
process = subprocess.Popen(
benchmark_command, env=new_env, stdin=input_file.open(), stdout=subprocess.PIPE)
lines = []
for line in process.stdout:
print(line.decode(), end='')
results.append(Result(lines, thread))
print("Completed run for {} threads".format(thread))