title: How do I terminate this subprocess with a timeout?
body: <p>I know the question on terminating a subprocess call has been asked a few times, including <a href="https://stackoverflow.com/questions/337863/python-popen-and-select-waiting-for-a-process-to-terminate-or-a-timeout?rq=1">here</a> but, trying to follow those answers, I can't seem to get mine to exit if the script hangs (in my case I am executing a phantomjs script). For instance, if I try to load a non-existent jquery file in my phantom code, the script will hang, even though I have a timeout. Here's my code:</p>
  
  <pre><code>def kill_proc():
      if p.poll() != 0:
          process.kill()
  
  
  p = subprocess.Popen(['phantomjs','file.js'],stdout=subprocess.PIPE)
      out, phantomError = p.communicate()
  
  t = Timer(5, kill_proc) # should kill it after 5 seconds
  t.start()
  p.wait()
  </code></pre>
  
  <p>My phantomjs script (a work in progress):</p>
  
  <pre><code>var page = require('webpage').create();
  page.includeJs("http://localhost/jquery.js",function(){ 
      phantom.exit();
  });
  </code></pre>