title: Terminate a multi-thread python program
body: <p>How to make a multi-thread python program response to Ctrl+C key event?</p>
  
  <p><strong>Edit:</strong> The code is like this:</p>
  
  <pre><code>import threading
  current = 0
  
  class MyThread(threading.Thread):
      def __init__(self, total):
          threading.Thread.__init__(self)
          self.total = total
  
      def stop(self):
          self._Thread__stop()
  
      def run(self):
          global current
          while current&lt;self.total:
              lock = threading.Lock()
              lock.acquire()
              current+=1
              lock.release()
              print current
  
  if __name__=='__main__':
  
      threads = []
      thread_count = 10
      total = 10000
      for i in range(0, thread_count):
          t = MyThread(total)
          t.setDaemon(True)
          threads.append(t)
      for i in range(0, thread_count):
          threads[i].start()
  </code></pre>
  
  <p>I tried to remove join() on all threads but it still doesn't work. Is it because the lock segment inside each thread's run() procedure?</p>
  
  <p><strong>Edit:</strong> The above code is supposed to work but it always interrupted when current variable was in 5,000-6,000 range and through out the errors as below</p>
  
  <pre><code>Exception in thread Thread-4 (most likely raised during interpreter shutdown):
  Traceback (most recent call last):
    File "/usr/lib/python2.5/threading.py", line 486, in __bootstrap_inner
    File "test.py", line 20, in run
  &lt;type 'exceptions.TypeError'&gt;: unsupported operand type(s) for +=: 'NoneType' and 'int'
  Exception in thread Thread-2 (most likely raised during interpreter shutdown):
  Traceback (most recent call last):
    File "/usr/lib/python2.5/threading.py", line 486, in __bootstrap_inner
    File "test.py", line 22, in run
  </code></pre>