diff py/tempserver.py @ 531:fd0fd9f947a1

improve exception handling
author Matt Johnston <matt@ucc.asn.au>
date Sat, 11 Apr 2015 21:09:13 +0800
parents 73257bae83ea
children 19569cb5ed46
line wrap: on
line diff
--- a/py/tempserver.py	Sat Apr 11 00:16:05 2015 +0800
+++ b/py/tempserver.py	Sat Apr 11 21:09:13 2015 +0800
@@ -24,7 +24,7 @@
         self.readings = []
         self.current = (None, None)
         self.fridge = None
-        self._wakeup = asyncio.Event()
+        self._wakeup = asyncio.Condition()
 
     def __enter__(self):
         self.params = params.Params()
@@ -51,12 +51,11 @@
             self.sensors.run(),
             self.uploader.run(),
         )
+
         loop = asyncio.get_event_loop()
-        try:
-            loop.run_until_complete(asyncio.wait(tasks))
-            # not reached
-        except KeyboardInterrupt:
-            pass
+        result_tasks = loop.run_until_complete(asyncio.wait(tasks, return_when=asyncio.FIRST_EXCEPTION))
+        # use the results so that exceptions get thrown
+        [t.result() for x in result_tasks for t in x]
 
     def now(self):
         return utils.monotonic_time()
@@ -90,25 +89,33 @@
 
     def current_temps(self):
         """ returns (wort_temp, fridge_temp) tuple """
+        D("current: %s" % str(self.current))
         return self.current
 
+    @asyncio.coroutine
     def sleep(self, timeout):
         """ sleeps for timeout seconds, though wakes if the server's config is updated """
-        asyncio.wait_for(self._wakeup, timeout=timeout)
+        # XXX fixme - we should wake on _wakeup but asyncio Condition with wait_for is a bit broken? 
+        # https://groups.google.com/forum/#!topic/python-tulip/eSm7rZAe9LM
+        # For now we just sleep, ignore the _wakeup
+        yield from asyncio.sleep(timeout)
         
+    @asyncio.coroutine
     def _reload_signal(self):
         try:
             self.params.load()
             L("Reloaded.")
-            self._wakeup.set()
-            self._wakeup.clear()
+            yield from self._wakeup.acquire()
+            self._wakeup.notify_all()
+            self._wakeup.release()
         except Error as e:
             W("Problem reloading: %s" % str(e))
 
 def setup_logging():
     logging.basicConfig(format='%(asctime)s %(message)s', 
             datefmt='%m/%d/%Y %I:%M:%S %p',
-            level=logging.INFO)
+            level=logging.DEBUG)
+    logging.getLogger("asyncio").setLevel(logging.DEBUG)
 
 def start():
     with Tempserver() as server: