I recently added a RPC backend to an existing single-threaded application as shown below
lock = Lock() def update_state(address, value): with lock: state[address] = value class ExistingClass: def __init__(self, *args, **kwargs): thread = Thread(target=self.start_listener) thread.start() def start_listener(self): self.server = SimpleXMLRPCServer(("localhost", 8002) , allow_none=True) self.server.register_function(update_state, "update_state") self.server.serve_forever() def read_state(self, address): with lock: return state[address] def write_state(self, address, value): with lock: return state[address] = value ec = ExistingClass() ec.server.shutdown() The RPC backend is run in a separate thread to allow the application to keep running.
The problem is that shutting down the application requires explicitly stopping the server and the function SimpleXMLRPCServer.shutdown() does not work after a client has called the PRC update_state i.e. the command ps shows that the process is still alive post shutdown.
Initially I thought the problem could be due to the absence of a lock but that didn't resolve the problem.