0

I have a program that can be considered as a black box. I can't modify it, and as outputs it continuously writes in the terminal where it has been executed, as well as in an output text file. I would like to use python to run this program, read the output text file every 30 seconds or so, and do things depending on some postprocessing. The program runs a numerical simulation, and based on the outputs I'd like to eventually kill it and restart a simulation with different parameters.

From what I have read, the subProcess.Popen command would be ideal. However I don't understand how to make it work. When I run the following, nothing happens:

import subprocess
cmd_str="./my_program";
p=subprocess.Popen(cmd_str);

If I use subprocess.run instead of .Popen, my program runs but not in the background. The program also runs if after the line "p=subprocess.Popen(cmd_str)" I add: "p.communicate()". It then basically behaves as if I had used ".run": the rest of the code is the python code is not executing.

How can I run this in the background ?

squille
  • 243
  • 1
  • 2
  • 10
  • Try looking at [How to start a background process in Python?](https://stackoverflow.com/questions/1196074/how-to-start-a-background-process-in-python) To close the process use `.terminate`. – Alias Cartellano Apr 19 '23 at 15:47

0 Answers0