Search code examples
pythoncherrypy

How to shutdown cherrypy from within?


I am developing on cherrypy, I start it from a python script.

For better development I wonder what is the correct way to stop cherrypy from within the main process (and not from the outside with ctrl-c or SIGTERM).

I assume I have to register a callback function from the main application to be able to stop the cherrypy main process from a worker thread.

But how do I stop the main process from within?


Solution

  • import sys
    class MyCherryPyApplication(object):
    
      def default(self):
        sys.exit()
      default.exposed = True
    
    cherrypy.quickstart(MyCherryPyApplication())
    

    Putting a sys.exit() in any request handler exits the whole server

    I would have expected this only terminates the current thread, but it terminates the whole server. That's what I wanted.