I currently have the following Python code:
my_process = subprocess.Popen(["cmd.exe", "/c", "unchangable_long_running_script.bat"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
for line in iter(my_process.stdout.readline, b""):
print(f">>> {line.rstrip()}")
if b"Setup complete" in line:
break
print("All done!")
It kicks off a long-running Windows batch script (that I cannot modify) and when a specific phrase is encountered, indicating that all is well, my Python script will continue. So the console output when running the above script is something like:
>>> Doing stuff
>>> Doing more stuff
>>> Setup complete
All done!
The problem that I have, is that I would like for the subprocess to continue beyond the lifetime of my Python script.
In order to achieve this, I can use the flags as described in this answer, but then I run into the problem that DETACHED_PROCESS
causes the batch script to be run in a separate console to the parent, as per the documentation. In this case, the stdout is now confined to that new console and is inaccessible to my Python script.
tldr; Is there a way to start a detached process in Python, but still read the stdout from that process in order to determine when the detached process has reached a certain point in its execution?