title: Popen waiting for child process even when the immediate child has terminated
body: <p>I'm working with Python 2.7 on Windows 8/XP.</p>
  <p>I have a program A that runs another program B using the following code:</p>
  <pre><code>p = Popen([&quot;B&quot;], stdout=PIPE, stderr=PIPE)
  stdout, stderr = p.communicate()
  return
  </code></pre>
  <p>B runs a batch script C. C is a long running script and I want B to exit even though C has not finished. I have done it using the following code (in B):</p>
  <pre><code>p = Popen([&quot;C&quot;])
  return
  </code></pre>
  <p>When I run B, it works as expected. When I run A however, I expected it to exit when B exits. But A waits until C exits even though B has already exitted. Any ideas on what's happening and what possible solutions could be?</p>
  <p>Unfortunately, the obvious solution of changing A to look like B is not an option.</p>
  <p>Here is a functional sample code to illustrate this issue:
  <a href="https://www.dropbox.com/s/cbplwjpmydogvu2/popen.zip?dl=1" rel="nofollow noreferrer">https://www.dropbox.com/s/cbplwjpmydogvu2/popen.zip?dl=1</a></p>
  <p>The zip file consists of the following files with the following contents:</p>
  <p><code>A.py</code></p>
  <pre class="lang-py prettyprint-override"><code>from subprocess import PIPE, Popen
  import sys
  
  def log(line):
      with open(&quot;log.txt&quot;, &quot;a&quot;) as logfile:
          logfile.write(line)
  
  log(&quot;\r\n\r\nA: I'll wait for B\r\n&quot;)
  
  p = Popen([&quot;C:\\Python27\\python.exe&quot;, &quot;B.py&quot;], stdout=PIPE, stderr=PIPE)
  stdout, stderr = p.communicate()
  
  log(&quot;A: Done.\r\n&quot;)
  sys.exit(0)
  </code></pre>
  <p><code>B.py</code></p>
  <pre class="lang-py prettyprint-override"><code>from subprocess import Popen, PIPE
  import sys
  
  def log(line):
      with open(&quot;log.txt&quot;, &quot;a&quot;) as logfile:
          logfile.write(line)
  
  log(&quot;B: launching C\r\n&quot;)
  
  p = Popen([&quot;C.bat&quot;])
  
  log(&quot;B: Not waiting for C at all. bye!\r\n&quot;)
  sys.exit(0)
  </code></pre>
  <p><code>C.bat</code></p>
  <pre><code>@echo off
  echo C: Start long running task : %time% &gt;&gt;  &quot;log.txt&quot;
  ping -n 10 127.0.0.1&gt;nul
  echo C: Stop long running task : %time% &gt;&gt;  &quot;log.txt&quot;
  </code></pre>
  <p>Any input is much appreciated.</p>