我HTTPServer
在一个单独的线程中运行我(使用无法停止线程的线程模块...)并希望在主线程也关闭时停止提供请求.
Python文档声明它BaseHTTPServer.HTTPServer
是一个子类SocketServer.TCPServer
,它支持一个shutdown
方法,但它缺少HTTPServer
.
整个BaseHTTPServer
模块的文档很少:(
我应该首先说"我可能不会自己这样做,但我过去也有".serve_forever(来自SocketServer.py)方法如下所示:
def serve_forever(self): """Handle one request at a time until doomsday.""" while 1: self.handle_request()
你可以替换(在子类)while 1
有while self.should_be_running
,并从不同的线程修改该值.就像是:
def stop_serving_forever(self): """Stop handling requests""" self.should_be_running = 0 # Make a fake request to the server, to really force it to stop. # Otherwise it will just stop on the next request. # (Exercise for the reader.) self.make_a_fake_request_to_myself()
编辑:我挖出了我当时使用的实际代码:
class StoppableRPCServer(SimpleXMLRPCServer.SimpleXMLRPCServer): stopped = False allow_reuse_address = True def __init__(self, *args, **kw): SimpleXMLRPCServer.SimpleXMLRPCServer.__init__(self, *args, **kw) self.register_function(lambda: 'OK', 'ping') def serve_forever(self): while not self.stopped: self.handle_request() def force_stop(self): self.server_close() self.stopped = True self.create_dummy_request() def create_dummy_request(self): server = xmlrpclib.Server('http://%s:%s' % self.server_address) server.ping()
在我的python 2.6安装中,我可以在底层TCPServer上调用它 - 它仍在你的内部HTTPServer
:
TCPServer.shutdown >>> import BaseHTTPServer >>> h=BaseHTTPServer.HTTPServer(('',5555), BaseHTTPServer.BaseHTTPRequestHandler) >>> h.shutdown> >>>
我想你可以用 [serverName].socket.close()
另一种基于http://docs.python.org/2/library/basehttpserver.html#more-examples的方法是:代替serve_forever(),只要满足条件,就保持服务,服务器检查每个请求之前和之后的条件.例如:
import CGIHTTPServer import BaseHTTPServer KEEP_RUNNING = True def keep_running(): return KEEP_RUNNING class Handler(CGIHTTPServer.CGIHTTPRequestHandler): cgi_directories = ["/cgi-bin"] httpd = BaseHTTPServer.HTTPServer(("", 8000), Handler) while keep_running(): httpd.handle_request()
事件循环在SIGTERM,Ctrl+ C或shutdown()
调用时结束.
server_close()
必须在server_forever()
关闭侦听套接字后调用.
import http.server
class StoppableHTTPServer(http.server.HTTPServer):
def run(self):
try:
self.serve_forever()
except KeyboardInterrupt:
pass
finally:
# Clean-up server (close socket, etc.)
self.server_close()
简单的服务器可以通过用户操作停止(SIGTERM,Ctrl+ C,...):
server = StoppableHTTPServer(("127.0.0.1", 8080),
http.server.BaseHTTPRequestHandler)
server.run()
服务器在线程中运行:
import threading
server = StoppableHTTPServer(("127.0.0.1", 8080),
http.server.BaseHTTPRequestHandler)
# Start processing requests
thread = threading.Thread(None, server.run)
thread.start()
# ... do things ...
# Shutdown server
server.shutdown()
thread.join()
在python 2.7中,调用shutdown()可以工作,但前提是你通过serve_forever服务,因为它使用异步选择和轮询循环.用handle_request()运行你自己的循环讽刺地排除了这个功能,因为它意味着一个愚蠢的阻塞调用.
从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: # 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()
下面是我的代码的一部分,用于从另一个线程执行阻塞关闭,使用事件等待完成:
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() # wait for thread to die for a bit, then give up raising an exception. if not self._webserver_died.wait(5): raise ValueError("couldn't kill webserver")