Proper shutdown of server via threaded call to shutdown().

pull/7/head
Ferry Boender 9 years ago
parent 81e3b480a8
commit 1c43496be3
  1. 23
      src/scriptform.py

@ -32,6 +32,7 @@ import signal
import time import time
import errno import errno
import logging import logging
import thread
html_header = u'''<html> html_header = u'''<html>
@ -190,6 +191,7 @@ class ScriptForm:
self.log = logging.getLogger('SCRIPTFORM') self.log = logging.getLogger('SCRIPTFORM')
self.get_form_config() # Init form config so it can raise errors about problems. self.get_form_config() # Init form config so it can raise errors about problems.
self.websrv = None self.websrv = None
self.running = False
def get_form_config(self): def get_form_config(self):
""" """
@ -233,22 +235,27 @@ class ScriptForm:
def run(self, listen_addr='0.0.0.0', listen_port=80): def run(self, listen_addr='0.0.0.0', listen_port=80):
""" """
Start the webserver on address `listen_addr` and port `listen_port`. Start the webserver on address `listen_addr` and port `listen_port`.
This call is blocking and will never return unless the user hits This call is blocking until the user hits Ctrl-c, the shutdown() method
Ctrl-c. is called or something like SystemExit is raised in a handler.
""" """
ScriptFormWebApp.scriptform = self ScriptFormWebApp.scriptform = self
self.httpd = ThreadedHTTPServer((listen_addr, listen_port), ScriptFormWebApp) self.httpd = ThreadedHTTPServer((listen_addr, listen_port), ScriptFormWebApp)
self.httpd.daemon_threads = True
self.log.info("Listening on {0}:{1}".format(listen_addr, listen_port)) self.log.info("Listening on {0}:{1}".format(listen_addr, listen_port))
self.running = True
self.httpd.serve_forever() self.httpd.serve_forever()
self.running = False
def shutdown(self): def shutdown(self):
self.log.info("Attempting server shutdown") self.log.info("Attempting server shutdown")
self.log.info(self.websrv) def t_shutdown(sf):
# FIXME: This is not the cleanest way to exit. shutdown() is called by sf.log.info(self.websrv)
# the atexit and signal handler. Ideally, we should call sf.httpd.socket.close() # Undocumented requirement to shut the server
# self.httpd.shutdown(), but that doesn't work because we're in the sf.httpd.shutdown()
# same thread. # We need to spawn a new thread in which the server is shut down,
raise SystemExit() # because doing it from the main thread blocks, since the server is
# wainting for connections..
t = thread.start_new_thread(t_shutdown, (self, ))
class FormConfig: class FormConfig:

Loading…
Cancel
Save