How to launch a background process in Python with a delayed invocation?
<p>Ok, this is a bit of a thorny problem. I need to launch a backgrounded process that will (1) wait N secs, and then (2) execute some command. Additionally, I need to capture the pid of the background process itself, because when the parent process finishes it will kill the backgrounded process if necessary. It looks a bit like this in shell syntax:</p>

<pre><code>  (sleep 15 &amp;&amp; run_some_cmd) &amp; # launch bg process
  bg_pid=$!                    # capture the bg pid
  # do some stuff here...     
  kill -9 $bg_pid              # and kill the bg_pid if necessary
</code></pre>

<p>So that's the launch in shell. However, I'm trying to do this in Python. The tricky thing is that since I need to capture the bg pid, I can't use os.system() or os.fork(), since in each case the parent process does not have access to the pid of the child. I'm trying to get it to work with subprocess.Popen(), but it's a bit tricky given the "sleep 15" portion.</p>

<p>Any ideas?</p>
