From b9212514bd07734cb3d60197b4d54b2ede2218f6 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Tue, 8 Oct 2013 19:00:10 -0700 Subject: [PATCH] Converted Timer and HTTP Interface threads to daemons for cleaner shutdown. --- homeassistant/__init__.py | 14 ++------------ homeassistant/httpinterface.py | 24 ++++-------------------- homeassistant/test.py | 9 +-------- 3 files changed, 7 insertions(+), 40 deletions(-) diff --git a/homeassistant/__init__.py b/homeassistant/__init__.py index 725402f771d..2e8c88561a3 100644 --- a/homeassistant/__init__.py +++ b/homeassistant/__init__.py @@ -17,7 +17,6 @@ logging.basicConfig(level=logging.INFO) ALL_EVENTS = '*' EVENT_START = "start" -EVENT_SHUTDOWN = "shutdown" EVENT_STATE_CHANGED = "state_changed" EVENT_TIME_CHANGED = "time_changed" @@ -41,9 +40,6 @@ def start_home_assistant(eventbus): time.sleep(1) except KeyboardInterrupt: - print "" - eventbus.fire(EVENT_SHUTDOWN) - break def ensure_list(parameter): @@ -218,31 +214,25 @@ class Timer(threading.Thread): def __init__(self, eventbus): threading.Thread.__init__(self) + self.daemon = True self.eventbus = eventbus - self._stop = threading.Event() eventbus.listen(EVENT_START, lambda event: self.start()) - eventbus.listen(EVENT_SHUTDOWN, lambda event: self._stop.set()) def run(self): """ Start the timer. """ logging.getLogger(__name__).info("Timer:starting") - now = datetime.now() - while True: while True: time.sleep(1) now = datetime.now() - if self._stop.isSet() or now.second % TIMER_INTERVAL == 0: + if now.second % TIMER_INTERVAL == 0: break - if self._stop.isSet(): - break - self.eventbus.fire(EVENT_TIME_CHANGED, {'now':now}) class HomeAssistantException(Exception): diff --git a/homeassistant/httpinterface.py b/homeassistant/httpinterface.py index e9ccb2102a0..015a129017e 100644 --- a/homeassistant/httpinterface.py +++ b/homeassistant/httpinterface.py @@ -31,9 +31,7 @@ import logging from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer from urlparse import urlparse, parse_qs -import requests - -from . import EVENT_START, EVENT_SHUTDOWN +from . import EVENT_START SERVER_PORT = 8123 @@ -49,6 +47,8 @@ class HTTPInterface(threading.Thread): server_port=None, server_host=None): threading.Thread.__init__(self) + self.daemon = True + if not server_port: server_port = SERVER_PORT @@ -64,29 +64,13 @@ class HTTPInterface(threading.Thread): self.server.statemachine = statemachine self.server.api_password = api_password - self._stop = threading.Event() - eventbus.listen(EVENT_START, lambda event: self.start()) - eventbus.listen(EVENT_SHUTDOWN, lambda event: self.stop()) def run(self): """ Start the HTTP interface. """ self.server.logger.info("Starting") - while not self._stop.is_set(): - self.server.handle_request() - - - def stop(self): - """ Stop the HTTP interface. """ - self._stop.set() - - # Trigger a fake request to get the server to quit - try: - requests.get("http://127.0.0.1:{}".format(SERVER_PORT), - timeout=0.001) - except requests.exceptions.RequestException: - pass + self.server.serve_forever() class RequestHandler(BaseHTTPRequestHandler): """ Handles incoming HTTP requests """ diff --git a/homeassistant/test.py b/homeassistant/test.py index 723ed63b7ed..197c4796db8 100644 --- a/homeassistant/test.py +++ b/homeassistant/test.py @@ -11,7 +11,7 @@ import time import requests -from . import EventBus, StateMachine, EVENT_START, EVENT_SHUTDOWN +from . import EventBus, StateMachine, EVENT_START from .httpinterface import HTTPInterface, SERVER_PORT @@ -45,13 +45,6 @@ class TestHTTPInterface(unittest.TestCase): cls.eventbus = EventBus() cls.statemachine = StateMachine(cls.eventbus) - @classmethod - def tearDownClass(cls): # pylint: disable=invalid-name - """ things to be run when tests are done. """ - cls.eventbus.fire(EVENT_SHUTDOWN) - - time.sleep(1) - def test_debug_interface(self): """ Test if we can login by comparing not logged in screen to logged in screen. """