In python 2.7, the shutdown () call works, but only if you serve through serve_forever, because it uses async select and a polling loop. Carrying out our own loop using handle_request (), ironically, eliminates this functionality because it involves a dumb blocking call.
With SocketServer.py BaseServer:
def serve_forever(self, poll_interval=0.5): """Handle one request at a time until shutdown. Polls for shutdown every poll_interval seconds. Ignores self.timeout. If you need to do periodic tasks, do them in another thread. """ self.__is_shut_down.clear() try: while not self.__shutdown_request:
Here is part of my code to block shutdown from another thread, using an event to wait for completion:
class MockWebServerFixture(object): def start_webserver(self): """ start the web server on a new thread """ self._webserver_died = threading.Event() self._webserver_thread = threading.Thread( target=self._run_webserver_thread) self._webserver_thread.start() def _run_webserver_thread(self): self.webserver.serve_forever() self._webserver_died.set() def _kill_webserver(self): if not self._webserver_thread: return self.webserver.shutdown()
jsalter Mar 18 '14 at 23:38 2014-03-18 23:38
source share