diff beermon.py @ 2:17bc5535bbb7

Wait on a condition var instead of using time.sleep() - this works better with signals in a multithreaded environment (ie the signal will interrupt the wait) A few other misc tidy ups.
author darius
date Mon, 24 Sep 2007 13:31:15 +0000
parents c0b01c8c63eb
children 1af7c85d5a0e
line wrap: on
line diff
--- a/beermon.py	Mon Sep 24 04:05:52 2007 +0000
+++ b/beermon.py	Mon Sep 24 13:31:15 2007 +0000
@@ -4,7 +4,7 @@
 # Monitor & control fermenter temperature 
 # v1.0
 #
-# $Id: beermon.py,v 1.2 2007/09/24 04:05:52 darius Exp $
+# $Id: beermon.py,v 1.3 2007/09/24 13:31:15 darius Exp $
 #
 # Depends on: Python 2.3 (I think)
 #
@@ -36,7 +36,7 @@
 ############################################################################
 
 
-import pexpect, re, threading, time, logging
+import pexpect, re, threading, time, logging, sys, traceback
 from logging.handlers import RotatingFileHandler
 
 class ROMReadError(Exception):
@@ -51,6 +51,8 @@
         self.m = m
         global log
         log = _log
+        self.cv = threading.Condition()
+        self.cv.acquire()
         
     def doit(self):
         log.debug("target temperature - %3.2f" % (self.targetTemp))
@@ -65,8 +67,8 @@
         log.debug("Fermenter	Fridge	Ambient	State	New State")
         while True:
             if (self.m.lastUpdate == 0):
-                print "%s Invalid data" % (time.asctime())
-                time.sleep(30)
+                log.debug("Invalid data")
+                self.cv.wait(self.pollInterval)
                 self.m.setState('idle')
                 continue
 
@@ -98,7 +100,7 @@
             if (nextState != "-"):
                 self.m.setState(nextState)
                 
-            time.sleep(self.pollInterval)
+            self.cv.wait(self.pollInterval)
             
 
 class MonitorDev(threading.Thread):
@@ -278,7 +280,6 @@
 
 def initLog():
     # Init our logging
-    global log
     log = logging.getLogger("monitor")
 
     # Default to warts and all logging
@@ -302,17 +303,19 @@
     return(log)
 
 def main():
-    import sys, traceback, beermon
-
-    exitCode = 0
+    import beermon
     
-    initLog()
+    global log
+    log = initLog()
 
     log.debug("=== Initing ===")
-    log.debug("$Id: beermon.py,v 1.2 2007/09/24 04:05:52 darius Exp $")
-    m = beermon.MonitorDev()
+    log.debug("$Id: beermon.py,v 1.3 2007/09/24 13:31:15 darius Exp $")
 
+    m = None
+    exitCode = 0
     try:
+        m = beermon.MonitorDev()
+
         c = beermon.Control(m, log)
         # Wait for the first temperature readings to come through, saves
         # getting an 'invalid data' message
@@ -323,16 +326,17 @@
     except KeyboardInterrupt:
         log.debug("Exiting due to keyboard interrupt")
 
-    #except Exception, e:
-    #    log.debug("Something went wrong, details below")
-    #    log.debug(e)
-    #    #log.debug(reduce(lambda x, y: x + y, traceback.format_exception(
-    #    #    sys.last_type, sys.last_value, sys.last_traceback)))
-    #    exitCode = 1
+    except Exception, e:
+        log.debug("Something went wrong, details below")
+        log.debug(e)
+        #log.debug(reduce(lambda x, y: x + y, traceback.format_exception(
+        #    sys.last_type, sys.last_value, sys.last_traceback)))
+        exitCode = 1
         
     finally:
         # Make sure we try and turn it off if something goes wrong
-        m.setState('idle')
+        if (m != None):
+            m.setState('idle')
 
     sys.exit(exitCode)