Search code examples
pythonmultithreadingcherrypy

Make CherryPy exit immediately upon receiving a keyboard interrupt?


Is there an easy way of making CherryPy perform an os._exit(0) once it gets a ^C interrupt?

It would normally wait for all the threads to terminate which is not useful for debugging as I expect it to shut down immediately.


Solution

  • See https://cherrypy.readthedocs.org/en/3.3.0/refman/process/plugins/signalhandler.html.

    handlers = {'SIGTERM': self.bus.exit,
            'SIGHUP': self.handle_SIGHUP,
            'SIGUSR1': self.bus.graceful,
           }
    

    are the default handlers; SIGINT from Ctrl-C isn't there (so I don't know if it's going the same as SIGTERM or what) but you could add it with whatever handler you want, including one that calls sys._exit.