What would be the easiest way of creating a Python server (XML-RPC Server) that shuts itself after sometime being idle?
I was thinking to do it like this but I don’t know what to do in the todo’s:
from SimpleXMLRPCServer import SimpleXMLRPCServer
# my_paths variable
my_paths = []
# Create server
server = SimpleXMLRPCServer(("localhost", 7789))
server.register_introspection_functions()
# TODO: set the timeout
# TODO: set a function to be run on timeout
class MyFunctions:
def add_path(paths):
for path in paths:
my_paths.append(path)
return "done"
def _dispatch(self, method, params):
if method == 'add_path':
# TODO: reset timeout
return add_path(*params)
else:
raise 'bad method'
server.register_instance(MyFunctions())
# Run the server's main loop
server.serve_forever()
I also tried to explore signal.alarm() following the example here but it won’t run under Windows throwing AttributeError: 'module' object has no attribute 'SIGALRM' at me.
Thanks.
You can create your own server class that extends
SimpleXMLRPCServerin order to shutdown when idle for sometime.Now, you can use this class instead to make your server object.