title: Python - Launch detached process, wait for specific output in stdout, then continue
body: <p>I currently have the following Python code:</p>
  <pre><code>my_process = subprocess.Popen([&quot;cmd.exe&quot;, &quot;/c&quot;, &quot;unchangable_long_running_script.bat&quot;], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  for line in iter(my_process.stdout.readline, b&quot;&quot;):
      print(f&quot;&gt;&gt;&gt; {line.rstrip()}&quot;)
      if b&quot;Setup complete&quot; in line:
          break
  print(&quot;All done!&quot;)
  </code></pre>
  <p>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:</p>
  <pre><code>&gt;&gt;&gt; Doing stuff
  &gt;&gt;&gt; Doing more stuff
  &gt;&gt;&gt; Setup complete
  All done!
  </code></pre>
  <p>The problem that I have, is that I would like for the subprocess to continue beyond the lifetime of my Python script.</p>
  <p>In order to achieve this, I can use the flags as described in <a href="https://stackoverflow.com/a/13256908/1798547">this answer</a>, but then I run into the problem that <code>DETACHED_PROCESS</code> causes the batch script to be run in a separate console to the parent, as per <a href="https://docs.python.org/3/library/subprocess.html#subprocess.DETACHED_PROCESS" rel="nofollow noreferrer">the documentation</a>. In this case, the stdout is now confined to that new console and is inaccessible to my Python script.</p>
  <p><strong>tldr;</strong> 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?</p>