title: Exit all threads in case of Error in python
body: <p>I'm working on a python project were I want the same behavior as in <code>C</code> for my threads. In <code>C</code> when the main thread exit, it kills all other threads.</p>
  
  <p>The project contains a <code>TCP</code> error server that it used to get logs from other threads and other software .The <code>TCP</code> link is simplex.
  Some errors must involve the end of the whole program.</p>
  
  <ul>
  <li>For external software I can kill them using their <code>PID</code>.</li>
  <li>For other threads I've tried <code>sys._exit()</code>, sometimes it works, and sometimes some threads remain.</li>
  </ul>
  
  <p>If my other threads were looping I could use a <code>semaphore</code> or something like that, but it is only one iteration of a linear process.</p>
  
  <p>I've thought about the design pattern <code>Producer/Consumer</code> or add a lot of <code>lock.acquire()/lock.release()</code> but I think it will add more complexity and it imply to break the linear thread.</p>
  
  <p>I've had a look to other <code>Stackoverflow</code> question I've found those solutions:</p>
  
  <ul>
  <li>Use <code>sys._exit()</code> but its success rate is not 100%.</li>
  <li>Convert my threads into <code>subprocess</code> to kill them easily, but in my case I can't.</li>
  </ul>
  
  <p>I'm looking for a solution, a design pattern or something else to solve it.</p>
  
  <p>PS: I'm a <code>C</code> lover and each time I deal with Python I think to solutions as simple as to call <code>exit()</code> to kill all my threads.</p>