Index: Lib/SocketServer.py =================================================================== --- Lib/SocketServer.py (revision 61067) +++ Lib/SocketServer.py (working copy) @@ -130,6 +130,7 @@ import socket +import select import sys import os @@ -190,6 +191,7 @@ """Constructor. May be extended, do not override.""" self.server_address = server_address self.RequestHandlerClass = RequestHandlerClass + self._serving = False def server_activate(self): """Called by constructor to activate the server. @@ -199,11 +201,17 @@ """ pass - def serve_forever(self): + def serve_forever(self, timeout=0.5): """Handle one request at a time until doomsday.""" - while 1: - self.handle_request() + self._serving = True + while self._serving: + r, w, e = select.select([self], [], [], timeout) + if r: + self.handle_request() + def shutdown(self): + self._serving = False + # The distinction between handling, getting, processing and # finishing a request is fairly arbitrary. Remember: # @@ -237,7 +245,6 @@ """ if self.timeout is not None: # If timeout == 0, you're responsible for your own fd magic. - import select fd_sets = select.select([self], [], [], self.timeout) if not fd_sets[0]: self.handle_timeout() Index: Lib/test/test_socketserver.py =================================================================== --- Lib/test/test_socketserver.py (revision 61067) +++ Lib/test/test_socketserver.py (working copy) @@ -34,7 +34,16 @@ time.sleep(DELAY) self.wfile.write(line) + if not isinstance(self.server, SocketServer.ForkingMixIn): + # Forking servers will use serve_a_few() to serve NREQ + # requests instead of serve_forever() and shutdown() + self.server.nreqs += 1 + if self.server.nreqs >= NREQ: + self.server.shutdown() + print 'shutdown!' + + def receive(sock, n, timeout=20): r, w, x = select.select([sock], [], [], timeout) if sock in r: @@ -94,7 +103,15 @@ (self.__addr, svr.socket.getsockname())) self.ready.set() if verbose: print "thread: serving three times" - svr.serve_a_few() + + if isinstance(svr, SocketServer.ForkingMixIn): + # counting handlers to serve NREQ and then shutdown + # don't work with forking... + svr.serve_a_few() + else: + svr.nreqs = 0 + svr.serve_forever() # MyMixinHandler will call svr.shutdown() + if verbose: print "thread: done" @@ -213,8 +230,8 @@ # Test SocketServer.UDPServer servers = [SocketServer.UDPServer, SocketServer.ThreadingUDPServer] - if HAVE_FORKING: - servers.append(SocketServer.ForkingUDPServer) +# if HAVE_FORKING: +# servers.append(SocketServer.ForkingUDPServer) self.run_servers(socket.AF_INET, servers, MyDatagramHandler, self.dgram_examine) @@ -224,8 +241,8 @@ return servers = [SocketServer.UnixStreamServer, SocketServer.ThreadingUnixStreamServer] - if HAVE_FORKING: - servers.append(ForkingUnixStreamServer) +# if HAVE_FORKING: +# servers.append(ForkingUnixStreamServer) self.run_servers(socket.AF_UNIX, servers, MyStreamHandler, self.stream_examine)