title: Why can't this Python multiprocessing process be terminated from Kivy?
body: <p>I'm trying to run a django development server from within a Kivy application. This did work out quite well so far.</p>
  
  <p>Now i want to allow the user to continue working with the program while the server is running. My idea was to create a multiprocessing.Process for the httpd.serve_forever() to avoid a complete lock of the main program. Did work well. This is the code in my internal_django module:</p>
  
  <pre><code>import multiprocessing
  import os
  import time
  
  from wsgiref.simple_server import make_server
  
  def django_wsgi_application():
  
      PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
      settings_module = "djangosettings"#%s.djangosettings" % PROJECT_ROOT.split(os.sep)[-1]
  
      os.environ.update({"DJANGO_SETTINGS_MODULE":settings_module})
  
      from django.core.wsgi import get_wsgi_application
      application = get_wsgi_application()
  
      return application
  
  
  class Singleton(type):
      _instances = {}
      def __call__(cls, *args, **kwargs):
          if cls not in cls._instances:
              cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
          return cls._instances[cls]
  
  
  class DjangoServer():
      __metaclass__ = Singleton
  
      def start(self):
          self.httpd = make_server('', 8000, django_wsgi_application())
          self.server = multiprocessing.Process(target=self.httpd.serve_forever)
          self.server.start()
          print "Now serving on port 8000..."
          print "Server Process PID = %s" %self.server.pid
  
      def stop(self):
          print("shutdown initiated")
          print "Server Process PID = %s" %self.server.pid
          while self.server.is_alive():
              self.server.terminate()
              print("Server should have shut down")
              time.sleep(1)
          print("Server is_alive: %s" %self.server.is_alive())
          self.server.join()
          print("server process joined")
  
  
  
  if __name__ == "__main__":
      server = DjangoServer()
      server.start()
      time.sleep(3)
      server.stop()
  </code></pre>
  
  <p>When i run this code, everything works as expected. This is what is being put out in the console:</p>
  
  <pre><code>Now serving on port 8000...
  Server Process PID = 1406
  shutdown initiated
  Server Process PID = 1406
  Server should have shut down
  Server is_alive: False
  server process joined
  </code></pre>
  
  <p>Next step was to provide a way to stop the server from within the Kivy application. For that i just wanted to use my DjangoServer class as i did before:</p>
  
  <pre><code>from internal_django import DjangoServer
  
  class StartScreen(Screen): 
      def start_server(self):
          server = DjangoServer()
          server.start()
  
  
  class StopScreen(Screen):  
      def stop_server(self):
          server = DjangoServer()
          server.stop()
  </code></pre>
  
  <p>But when doing so, the process once started never quits. My first idea was that the Singleton did not work as expected, and that i try to quit the wrong process. but as you can see in the output, the PID's are identical. The server receives the terminate command, but just continues to work. This is what the console looks like:</p>
  
  <pre><code>Now serving on port 8000...
  Server Process PID = 1406
  shutdown initiated
  Server Process PID = 1406
  Server should have shut down
  Server should have shut down
  Server should have shut down
  Server should have shut down
  Server should have shut down
  Server should have shut down
  Server should have shut down
  Server should have shut down
  
  (and so on, until i manually kill the server process)
  </code></pre>
  
  <p>Am i using multiprocessing in a completely wrong way? Is Kivy somehow interfering with the process?</p>