Come interrompere BaseHTTPServer.serve_forever () in una sottoclasse BaseHTTPRequestHandler?

StackOverflow https://stackoverflow.com/questions/268629

  •  06-07-2019
  •  | 
  •  

Domanda

Sto eseguendo il mio HTTPServer in un thread separato (utilizzando il modulo threading che non ha modo di arrestare i thread ...) e desidero interrompere la pubblicazione delle richieste quando anche il thread principale si spegne.

La documentazione di Python afferma che BaseHTTPServer.HTTPServer è una sottoclasse di SocketServer.TCPServer , che supporta un metodo shutdown , ma manca in HTTPServer .

L'intero modulo BaseHTTPServer ha pochissima documentazione :(

È stato utile?

Soluzione

Dovrei iniziare dicendo che "probabilmente non lo farei da solo, ma in passato". Il metodo serve_forever (da SocketServer.py) è simile al seguente:

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

È possibile sostituire (nella sottoclasse) mentre 1 con mentre self.should_be_running e modificare quel valore da un thread diverso. Qualcosa del tipo:

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

Modifica: ho estratto il codice che ho usato in quel momento:

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

Altri suggerimenti

Nella mia installazione di Python 2.6, posso chiamarlo sul TCPServer sottostante - è ancora lì dentro il tuo HTTPServer :

TCPServer.shutdown


>>> import BaseHTTPServer
>>> h=BaseHTTPServer.HTTPServer(('',5555), BaseHTTPServer.BaseHTTPRequestHandler)
>>> h.shutdown
<bound method HTTPServer.shutdown of <BaseHTTPServer.HTTPServer instance at 0x0100D800>>
>>> 

Penso che tu possa usare [serverName[.socket.close()

Un altro modo per farlo, basato su http://docs.python.org/2 /library/basehttpserver.html#more-examples è: invece di serve_forever (), continua a servire fino a quando viene soddisfatta una condizione, con il server che controlla la condizione prima e dopo ogni richiesta. Ad esempio:

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

Il loop degli eventi termina con SIGTERM, Ctrl + C o quando viene chiamato shutdown () .

server_close () deve essere chiamato dopo server_forever () per chiudere il socket di ascolto.

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

Server semplice arrestabile con azione dell'utente (SIGTERM, Ctrl + C , ...):

server = StoppableHTTPServer(("127.0.0.1", 8080),
                             http.server.BaseHTTPRequestHandler)
server.run()

Server in esecuzione in un thread:

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

In python 2.7, la chiamata di shutdown () funziona, ma solo se si sta servendo tramite serve_forever, perché usa async select e un ciclo di polling. L'esecuzione del proprio ciclo con handle_request () esclude ironicamente questa funzionalità perché implica una stupida chiamata di blocco.

Dal BaseServer di SocketServer.py:

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

Ecco parte del mio codice per aver fatto l'arresto di un blocco da un altro thread, usando un evento per attendere il completamento:

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

Questo metodo che uso correttamente (Python 3) per arrestare il server dall'applicazione Web stessa (una pagina Web):

import http.server
import os
import re

class PatientHTTPRequestHandler(http.server.SimpleHTTPRequestHandler):
    stop_server = False
    base_directory = "/static/"
    # A file to use as an "server stopped user information" page.
    stop_command = "/control/stop.html"
    def send_head(self):
        self.path = os.path.normpath(self.path)
        if self.path == PatientHTTPRequestHandler.stop_command and self.address_string() == "127.0.0.1":
            # I wanted that only the local machine could stop the server.
            PatientHTTPRequestHandler.stop_server = True
            # Allow the stop page to be displayed.
            return http.server.SimpleHTTPRequestHandler.send_head(self)
        if self.path.startswith(PatientHTTPRequestHandler.base_directory):
            return http.server.SimpleHTTPRequestHandler.send_head(self)
        else:
            return self.send_error(404, "Not allowed", "The path you requested is forbidden.")

if __name__ == "__main__":
    httpd = http.server.HTTPServer(("127.0.0.1", 8080), PatientHTTPRequestHandler)
    # A timeout is needed for server to check periodically for KeyboardInterrupt
    httpd.timeout = 1
    while not PatientHTTPRequestHandler.stop_server:
        httpd.handle_request()

In questo modo, le pagine sono servite tramite l'indirizzo di base http: // localhost: 8080 / static / (esempio http: // localhost: 8080 / static / styles / common.css ) verrà fornito dal gestore predefinito, un accesso a http: // localhost: 8080 / control / stop.html dal computer del server visualizzerà stop.html quindi arrestare il server, sarà vietata qualsiasi altra opzione.

Ho provato la soluzione sopra possibile e ho finito per avere un "a volte" problema - in qualche modo non l'ha fatto davvero - così ho finito per creare una soluzione sporca che ha funzionato tutto il tempo per me:

Se tutto quanto sopra fallisce, allora la forza bruta uccide il tuo thread usando qualcosa del genere:

import subprocess
cmdkill = "kill $(ps aux|grep '<name of your thread> true'|grep -v 'grep'|awk '{print $2}') 2> /dev/null"
subprocess.Popen(cmdkill, stdout=subprocess.PIPE, shell=True)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top