当前位置:  开发笔记 > 编程语言 > 正文

如何在BaseHTTPRequestHandler子类中停止BaseHTTPServer.serve_forever()?

如何解决《如何在BaseHTTPRequestHandler子类中停止BaseHTTPServer.serve_forever()?》经验,为你挑选了6个好方法。

HTTPServer在一个单独的线程中运行我(使用无法停止线程的线程模块...)并希望在主线程也关闭时停止提供请求.

Python文档声明它BaseHTTPServer.HTTPServer是一个子类SocketServer.TCPServer,它支持一个shutdown方法,但它缺少HTTPServer.

整个BaseHTTPServer模块的文档很少:(



1> Ali Afshar..:

我应该首先说"我可能不会自己这样做,但我过去也有".serve_forever(来自SocketServer.py)方法如下所示:

def serve_forever(self):
    """Handle one request at a time until doomsday."""
    while 1:
        self.handle_request()

你可以替换(在子类)while 1while 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.7中查看BaseServer.serve_forever()源而言,我已经看到它已经实现了一个标志,即__shutdown_request和顶部的__is_shut_down` threading.Event。所以我在这里看到的实际答案是`allow_reuse_address = True`行。

2> gimel..:

在我的python 2.6安装中,我可以在底层TCPServer上调用它 - 它仍在你的内部HTTPServer:

TCPServer.shutdown


>>> import BaseHTTPServer
>>> h=BaseHTTPServer.HTTPServer(('',5555), BaseHTTPServer.BaseHTTPRequestHandler)
>>> h.shutdown
>
>>> 


请注意,即使在关闭后,套接字仍将侦听接口/端口(在Python v2.7上测试).你也应该调用:`h.socket.close()`.

3> 小智..:

我想你可以用 [serverName].socket.close()


这是对我有用的唯一方法,尝试使用server.shutdown()中断而不是some_flag.只有socket.close()实现了所需.

4> 小智..:

另一种基于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()



5> Vianney Baja..:

事件循环在SIGTERM,Ctrl+ Cshutdown()调用时结束.

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()



6> jsalter..:

在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")

推荐阅读
夏晶阳--艺术
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有