title: Python subprocess: callback when cmd exits
body: <p>I'm currently launching a programme using <code>subprocess.Popen(cmd, shell=TRUE)</code></p>
  
  <p>I'm fairly new to Python, but it 'feels' like there ought to be some api that lets me do something similar to:</p>
  
  <pre><code>subprocess.Popen(cmd, shell=TRUE,  postexec_fn=function_to_call_on_exit)
  </code></pre>
  
  <p>I am doing this so that <code>function_to_call_on_exit</code> can do something based on knowing that the cmd has exited (for example keeping count of the number of external processes currently running)</p>
  
  <p>I assume that I could fairly trivially wrap subprocess in a class that combined threading with the <code>Popen.wait()</code> method, but as I've not done threading in Python yet and it seems like this might be common enough for an API to exist, I thought I'd try and find one first.</p>
  
  <p>Thanks in advance :)</p>