title: subprocess with timeout: What to do after TimeoutExpired Exception?
body: <p>subprocess timeout=SECONDS don't work for me if I follow the advice of the python docs:</p>
  
  <pre><code># copy+pasteable snippet :-)
  import subprocess32 as subprocess
  import datetime
  
  now=datetime.datetime.now()
  pipe=subprocess.Popen('sleep 5', shell=True, stdout=subprocess.PIPE)
  try:
      pipe.communicate(timeout=1)
  except subprocess.TimeoutExpired, exc:
      time_delta=datetime.datetime.now()-now
      print(time_delta)
      assert time_delta&lt;datetime.timedelta(seconds=3), time_delta
      pipe.kill()
      outs, errs = pipe.communicate()
      time_delta=datetime.datetime.now()-now
      print(time_delta)
      assert time_delta&lt;datetime.timedelta(seconds=3), time_delta
      print('OK')
  </code></pre>
  
  <p>The docs advice to use pipe.kill() and pipe.communicate() after TimeoutExpired: <a href="https://docs.python.org/3/library/subprocess.html#subprocess.Popen.communicate" rel="nofollow">https://docs.python.org/3/library/subprocess.html#subprocess.Popen.communicate</a></p>
  
  <p>It does not work for me. The second <code>communicate()</code> does not return soon.</p>
  
  <p>Exception:</p>
  
  <pre><code>0:00:01.001234
  0:00:05.002919
  Traceback (most recent call last):
    File "/home/foo/src/test_timeout.py", line 16, in &lt;module&gt;
      assert time_delta&lt;datetime.timedelta(seconds=3), time_delta
  AssertionError: 0:00:05.002919
  </code></pre>
  
  <p>If I use <code>['sleep', '5']</code> instead of <code>shell=True</code>, then it works.
  If I don't supply <code>stdout=subprocess.PIPE</code>, then it works, too.</p>
  
  <p>I guess the shell does not react on the pipe.kill(). </p>
  
  <p>What is the best way to solve this?</p>