title: kill subprocess using signal.CTRL_C_EVENT without killing main
body: <p><strong>How can I kill a subprocess without also killing <strong>main</strong>?</strong></p>
  <p>I have a subprocess that can only be killed with <code>signal.CTRL_C_EVENT</code>. Performing the standard <code>os.kill(my_pid, signal.CTRL_C_EVENT)</code> kills the process but also causes my main to die despite having a differnet pid.</p>
  <p>My ultimate goal is to create the subprocess from within a unit test so i need the unit test to return <code>sys.exit(0)</code> after passing all tests.</p>
  <p><strong>child.py</strong></p>
  <pre><code>from time
  
  while True:
    print &quot;I am alive&quot;
    time.sleep(0.5)
  </code></pre>
  <p><strong>parent.py</strong></p>
  <pre><code>import os
  import signal
  import subprocess
  import time
  
  p = subprocess.Popen([&quot;child.py&quot;], shell=True)
  time.sleep(5)
  os.kill(p.pid, signal.CTRL_C_EVENT)
  
  print(&quot;This line doesn't print because the main thread dies&quot;)
  </code></pre>
  <p>::: update :::</p>
  <p>I am trying to run this in Windows. removing <code>shell=True</code> has no apparent effect. I am seeing a KeyboardInterrupt error in parent.py</p>
  <pre><code>Traceback (most recent call last)    
      File &quot;parent.py&quot;, line 23, in &lt;module&gt;
  KeyboardInterrupt
  </code></pre>
  <p>All of the following methods result in this same behavior</p>
  <ul>
  <li><code>os.kill</code></li>
  <li>send signal on <code>psutil.Process(os.getpid()).children()</code></li>
  <li><code>subprocess.call(['taskkill', ...</code></li>
  </ul>