创建在一段时间空闲后自行关闭的 Python 服务器(XML-RPC 服务器)的最简单方法是什么?
我想这样做,但我不知道在待办事项中该怎么做:
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()
我也尝试按照此处signal.alarm()
的示例进行探索,但它不会在 Windows 下运行,这会向我抛出。AttributeError: 'module' object has no attribute 'SIGALRM'
谢谢。