comparison py/tempserver.py @ 232:a01b7bccccd3

improve exception handling
author Matt Johnston <matt@ucc.asn.au>
date Sat, 11 Apr 2015 21:09:13 +0800
parents e39ed85d87a5
children 19569cb5ed46
comparison
equal deleted inserted replaced
231:e39ed85d87a5 232:a01b7bccccd3
22 class Tempserver(object): 22 class Tempserver(object):
23 def __init__(self): 23 def __init__(self):
24 self.readings = [] 24 self.readings = []
25 self.current = (None, None) 25 self.current = (None, None)
26 self.fridge = None 26 self.fridge = None
27 self._wakeup = asyncio.Event() 27 self._wakeup = asyncio.Condition()
28 28
29 def __enter__(self): 29 def __enter__(self):
30 self.params = params.Params() 30 self.params = params.Params()
31 self.fridge = fridge.Fridge(self) 31 self.fridge = fridge.Fridge(self)
32 self.uploader = uploader.Uploader(self) 32 self.uploader = uploader.Uploader(self)
49 tasks = ( 49 tasks = (
50 self.fridge.run(), 50 self.fridge.run(),
51 self.sensors.run(), 51 self.sensors.run(),
52 self.uploader.run(), 52 self.uploader.run(),
53 ) 53 )
54
54 loop = asyncio.get_event_loop() 55 loop = asyncio.get_event_loop()
55 try: 56 result_tasks = loop.run_until_complete(asyncio.wait(tasks, return_when=asyncio.FIRST_EXCEPTION))
56 loop.run_until_complete(asyncio.wait(tasks)) 57 # use the results so that exceptions get thrown
57 # not reached 58 [t.result() for x in result_tasks for t in x]
58 except KeyboardInterrupt:
59 pass
60 59
61 def now(self): 60 def now(self):
62 return utils.monotonic_time() 61 return utils.monotonic_time()
63 62
64 def set_sensors(self, sensors): 63 def set_sensors(self, sensors):
88 if len(self.readings) > config.MAX_READINGS: 87 if len(self.readings) > config.MAX_READINGS:
89 self.readings = self.readings[-config.MAX_READINGS:] 88 self.readings = self.readings[-config.MAX_READINGS:]
90 89
91 def current_temps(self): 90 def current_temps(self):
92 """ returns (wort_temp, fridge_temp) tuple """ 91 """ returns (wort_temp, fridge_temp) tuple """
92 D("current: %s" % str(self.current))
93 return self.current 93 return self.current
94 94
95 @asyncio.coroutine
95 def sleep(self, timeout): 96 def sleep(self, timeout):
96 """ sleeps for timeout seconds, though wakes if the server's config is updated """ 97 """ sleeps for timeout seconds, though wakes if the server's config is updated """
97 asyncio.wait_for(self._wakeup, timeout=timeout) 98 # XXX fixme - we should wake on _wakeup but asyncio Condition with wait_for is a bit broken?
99 # https://groups.google.com/forum/#!topic/python-tulip/eSm7rZAe9LM
100 # For now we just sleep, ignore the _wakeup
101 yield from asyncio.sleep(timeout)
98 102
103 @asyncio.coroutine
99 def _reload_signal(self): 104 def _reload_signal(self):
100 try: 105 try:
101 self.params.load() 106 self.params.load()
102 L("Reloaded.") 107 L("Reloaded.")
103 self._wakeup.set() 108 yield from self._wakeup.acquire()
104 self._wakeup.clear() 109 self._wakeup.notify_all()
110 self._wakeup.release()
105 except Error as e: 111 except Error as e:
106 W("Problem reloading: %s" % str(e)) 112 W("Problem reloading: %s" % str(e))
107 113
108 def setup_logging(): 114 def setup_logging():
109 logging.basicConfig(format='%(asctime)s %(message)s', 115 logging.basicConfig(format='%(asctime)s %(message)s',
110 datefmt='%m/%d/%Y %I:%M:%S %p', 116 datefmt='%m/%d/%Y %I:%M:%S %p',
111 level=logging.INFO) 117 level=logging.DEBUG)
118 logging.getLogger("asyncio").setLevel(logging.DEBUG)
112 119
113 def start(): 120 def start():
114 with Tempserver() as server: 121 with Tempserver() as server:
115 server.run() 122 server.run()
116 123