title: Close multi threaded application with KeyboardInterrupt
body: <p>I have an app with two threads. One is a pygame thread which runs a simple game, the other thread is a listening server which accepts messages which are used to control the game.</p>
  
  <p>Here is the stripped down pseudo code:</p>
  
  <pre><code>class ServerThread(threading.Thread):
      def run(self):
          class SingleTCPHandler(SocketServer.BaseRequestHandler):
              try:
                  while(1):
                  ...
                  #Receive messages from socket. Add them to pygame event queue
                  ...
              except KeyboardInterrupt:
                  sys.exit(0)
  ...
  ...
  class PygameThread(threading.Thread):
      def run(self):
      ...
      #pygame stuff
      ...
      #The following pygame code closed the app when closing the pygame window while running as a single thread
          for event in pygame.event.get():
                  if event.type==QUIT:
                      exit()
     ...
  try:
      server_thread = ServerThread()
      server_thread.start()
      pygame_thread = PygameThread()
      pygame_thread.start()
  except KeyboardInterrupt:
      sys.exit(0)
  </code></pre>
  
  <p>It seems that none of the exceptions are being caught. I've tried running just the server without the pygame thread and the:</p>
  
  <pre><code>        try:
              while(1):
              ...
              #Receive messages from socket. Add them to pygame event queue
              ...
          except KeyboardInterrupt:
              sys.exit(0)
  </code></pre>
  
  <p>doesn't respond to <code>Ctrl + c</code></p>
  
  <p>The pygame window standard close button (the little x op right) doesn't work anymore.</p>
  
  <p>And my try of a workaround:</p>
  
  <pre><code>try:
      server_thread = ServerThread()
      server_thread.start()
      pygame_thread = PygameThread()
      pygame_thread.start()
  except KeyboardInterrupt:
      sys.exit(0)
  </code></pre>
  
  <p>also isn't working.</p>
  
  <p>I'm looking for ideas to close the app without having to kill the shell from which the app has been started.</p>
  
  <p><strong>Updated</strong></p>
  
  <p>Based on the suggestion i did the following:
  Changed the former <code>while True</code> in both treads to <code>while not self.stop_requested:</code>.</p>
  
  <p>And also:</p>
  
  <pre><code>try:
      pygame_thread = PygameThread()
      pygame_thread.start()
      server_thread = ServerThread()
      server_thread.start()
  except KeyboardInterrupt:
      pygame_thread.stop_requested = True
      server_thread.stop_requested = True
  </code></pre>
  
  <p>It still isn't working. I also noticed that in the console which runs this code when I try to terminate with Ctrl+c, it only gets printed out.</p>
  
  <pre><code>alan@alan ~/.../py $ python main.py 
  ^C^C^C^C^C^C^C
  </code></pre>
  
  <p><strong>Updated</strong></p>
  
  <p>I did a little shortcut and changed the server thread to daemon, so it closes once the pygame window (which is tha pygame thread) is closed.</p>