title: Understanding subprocess.TimeoutExpired, want to kill a child process after a timeout occurs
body: <p>can somebody help me understand how the timeout parameter works in the subprocess module, and how to properly use the subprocess.TimeoutExpired exception?</p>
  
  <p>My usecase is I have a main runner program that runs a child process as part of its repeated routine. The child process is known to hang from time to time. I would like to prevent this hang from holding everything up.</p>
  
  <p>I was thinking i could use the timeout parameter to give the child only so long to run. However, in my sample programs below the behavior is not what I expected.  When parent.py is run, it does launch child.py and I can see the output of child.py counting up. After 4s parent.py does get the subprocess.TimeoutExpired exception, however output from child.py keeps coming. This leads me to believe that child.py process has not actually been killed.  The docs seem to suggest it will be killed however:</p>
  
  <blockquote>
    <p>The timeout argument is passed to Popen.wait(). If the timeout expires, the child process will be killed and then waited for again. The TimeoutExpired exception will be re-raised after the child process has terminated.</p>
  </blockquote>
  
  <p>So how might I finish this problem off? Do I need to somehow kill my child process myself when I receive the timeout exception?</p>
  
  <p>Thanks for the help.</p>
  
  <p>parent.py</p>
  
  <pre><code>#!/usr/bin/env python3
  
  import os
  import sys
  import subprocess
  
  p = subprocess.Popen("/path/to/python3 /path/to/child.py", shell=True)
  try:
      p.wait(timeout=4)
  except subprocess.TimeoutExpired:
      print("we got a timeout. exiting")
      sys.exit(1)
  </code></pre>
  
  <p>child.py</p>
  
  <pre><code>#!/usr/bin/env python3
  
  import os
  import sys
  import time
  
  for i in range(200):
      print("i is {}".format(i))
      time.sleep(1)
  </code></pre>