From df1f7e8af547bf5dbd48b592ff1752ab2077e162 Mon Sep 17 00:00:00 2001 From: Lakshmi Vyasarajan Date: Tue, 15 Mar 2011 12:04:59 +0530 Subject: [PATCH] Duplicated code from SocketServer.py to make sure shutdown works on py >=2.6 --- hyde/server.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/hyde/server.py b/hyde/server.py index e6165a5..0beaaca 100644 --- a/hyde/server.py +++ b/hyde/server.py @@ -4,6 +4,8 @@ Contains classes and utilities for serving a site generated from hyde. """ import os +import select +import threading import urlparse import urllib from SimpleHTTPServer import SimpleHTTPRequestHandler @@ -132,8 +134,46 @@ class HydeWebServer(HTTPServer): self.generator = Generator(self.site) self.request_time = datetime.strptime('1-1-1999', '%m-%d-%Y') self.regeneration_time = datetime.strptime('1-1-1998', '%m-%d-%Y') + self.__is_shut_down = threading.Event() + self.__shutdown_request = False HTTPServer.__init__(self, (address, port), HydeRequestHandler) +####### Code from python 2.7.1: Socket server +####### Duplicated to make sure shutdown works in Python v > 2.6 +####### + + 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: + # XXX: Consider using another file descriptor or + # connecting to the socket to wake this up instead of + # polling. Polling reduces our responsiveness to a + # shutdown request and wastes cpu at all other times. + r, w, e = select.select([self], [], [], poll_interval) + if self in r: + self._handle_request_noblock() + finally: + self.__shutdown_request = False + self.__is_shut_down.set() + + def shutdown(self): + """Stops the serve_forever loop. + + Blocks until the loop has finished. This must be called while + serve_forever() is running in another thread, or it will + deadlock. + """ + self.__shutdown_request = True + self.__is_shut_down.wait() + +############## Duplication End. def regenerate(self): """