title: Shutdown socketserver serve_forever() in one-thread Python application
body: <p>I know that socketserver has a method <code>shutdown()</code> which causes server to shut down but this only works in multiple threads application since the shutdown needs to be called from different thread than the thread where <code>serve_forever()</code> is running.</p>
  <p>My application handles only one request at time so I do not use separate threads for handling requests and I am unable to call <code>shutdown()</code> because it causes deadlock (it's not in the docs but it's stated directly in the source code of socketserver).</p>
  <p>I will paste here a simplified version of my code for better understanding:</p>
  <pre><code>import socketserver
  
  class TCPServerV4(socketserver.TCPServer):
    address_family = socket.AF_INET
    allow_reuse_address = True
  
  class TCPHandler(socketserver.BaseRequestHandler):
    def handle(self):
      try:
         data = self.request.recv(4096)
      except KeyboardInterrupt:
         server.shutdown()
  
  server = TCPServerV4((host, port), TCPHandler)
  server.server_forever()
  </code></pre>
  <p>I am aware that this code is not working. I just wanted to show you the thing I would like to accomplish - to shutdown server and quit the application while waiting for incoming data when the user presses <kbd>Ctrl</kbd><kbd>C</kbd>.</p>