3

I'm trying to make a small application using SimpleXMLRPCServer and I'm wondering how to properly exit it when receiving SIGTERM.

The reason is because I will make a start/stop script for the application later and I want it to perform various things before stopping.

My current code is:

import SimpleXMLRPCServer
import signal
import sys

if __name__ == "__main__":
    print "setting up xmlrpc server"
    server = SimpleXMLRPCServer.SimpleXMLRPCServer(("localhost", 8000))

    def signal_handler(signum, frame):
        print "received signal"
        server.server_close()

        # perform clean up, etc. here...

        print "exiting, gracefully"
        sys.exit(0)

    # signals
    signal.signal(signal.SIGTERM, signal_handler)
    signal.signal(signal.SIGHUP, signal_handler)
    signal.signal(signal.SIGINT, signal_handler)

    print "serving forever"
    server.serve_forever()

It is working, but I'm not quite sure that I'm doing it the "correct" way. Any thoughts or ideas?

Also, are there any other signals that I should be listening for?

juliomalegria
  • 24,229
  • 14
  • 73
  • 89
xit
  • 41
  • 3

0 Answers0