Browse Source

Duplicated code from SocketServer.py to make sure shutdown works on py >=2.6

main
Lakshmi Vyasarajan 13 years ago
parent
commit
df1f7e8af5
1 changed files with 40 additions and 0 deletions
  1. +40
    -0
      hyde/server.py

+ 40
- 0
hyde/server.py View File

@@ -4,6 +4,8 @@ Contains classes and utilities for serving a site
generated from hyde. generated from hyde.
""" """
import os import os
import select
import threading
import urlparse import urlparse
import urllib import urllib
from SimpleHTTPServer import SimpleHTTPRequestHandler from SimpleHTTPServer import SimpleHTTPRequestHandler
@@ -132,8 +134,46 @@ class HydeWebServer(HTTPServer):
self.generator = Generator(self.site) self.generator = Generator(self.site)
self.request_time = datetime.strptime('1-1-1999', '%m-%d-%Y') self.request_time = datetime.strptime('1-1-1999', '%m-%d-%Y')
self.regeneration_time = datetime.strptime('1-1-1998', '%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), HTTPServer.__init__(self, (address, port),
HydeRequestHandler) 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): def regenerate(self):
""" """


Loading…
Cancel
Save