Switch to multiprocessing

This commit is contained in:
Mike Cifelli 2022-12-25 12:33:06 -05:00
parent fc77feedb2
commit 75cea0753a
3 changed files with 52 additions and 51 deletions

View File

@ -1,8 +1,10 @@
import sys
import threading
from character_display import CharacterDisplay
from signal import signal, SIGTERM
from multiprocessing import Event
from multiprocessing import Process
from signal import signal
from signal import SIGTERM
from time import sleep
from time_display import TimeDisplay
from weather_display import WeatherDisplay
@ -16,34 +18,17 @@ except ImportError:
unicorn.rotation(180)
WEATHER_UPDATE_INTERVAL_IN_SECONDS = 10
WEATHER_UPDATE_INTERVAL_IN_SECONDS = 60
DISPLAY_UPDATE_INTERVAL_IN_SECONDS = 0.5
CONFIG_FILE = 'chutney.cfg'
def cleanExit(unicorn, haltEvent):
def _exit_(signum, frame):
unicorn.off()
haltEvent.set()
sys.exit(0)
return _exit_
def weatherLoop(haltEvent):
weatherUpdater = WeatherUpdater(configFile=CONFIG_FILE)
while not haltEvent.is_set():
weatherUpdater.updateWeather()
sleep(WEATHER_UPDATE_INTERVAL_IN_SECONDS)
def main():
haltEvent = threading.Event()
haltEvent = Event()
signal(SIGTERM, cleanExit(unicorn, haltEvent))
unicorn.brightness(0.3)
weatherThread = threading.Thread(target=weatherLoop, args=[haltEvent])
weatherThread = Process(target=weatherLoop, args=[haltEvent])
weatherThread.start()
characterDisplay = CharacterDisplay(unicorn)
@ -56,5 +41,24 @@ def main():
sleep(DISPLAY_UPDATE_INTERVAL_IN_SECONDS)
def weatherLoop(haltEvent):
weatherUpdater = WeatherUpdater(configFile=CONFIG_FILE)
while not haltEvent.is_set():
weatherUpdater.updateWeather()
haltEvent.wait(timeout=WEATHER_UPDATE_INTERVAL_IN_SECONDS)
weatherUpdater.clearWeather()
def cleanExit(unicorn, haltEvent):
def _exit_(signum, frame):
unicorn.off()
haltEvent.set()
sys.exit(0)
return _exit_
if __name__ == '__main__':
main()

View File

@ -11,15 +11,23 @@ class WeatherDisplay:
self.weatherFile = 'weather.txt'
def showWeather(self):
temperature = self.getTemperature()
if self.isTemperatureMissing(temperature):
self.characterDisplay.clearRow(self.topRow)
else:
self.showTemperature(list(temperature))
def getTemperature(self):
try:
self.showTemperature(
self.getTemperatureCharacters(self.getTemperature()))
except requests.exceptions.ConnectionError:
print('connection error', flush=True)
self.characterDisplay.clearRow(self.topRow)
except requests.exceptions.Timeout:
print('timeout', flush=True)
self.characterDisplay.clearRow(self.topRow)
with open(self.weatherFile) as file:
return file.readline().strip()
except Exception as e:
print(e, flush=True)
return 'error'
def isTemperatureMissing(self, temperature):
return temperature == 'error' or temperature == 'init' or temperature == 'halted'
def showTemperature(self, temperature):
if temperature != self.currentTemperature:
@ -38,16 +46,3 @@ class WeatherDisplay:
start = start + 4
self.currentTemperature = temperature
def getTemperature(self):
# TODO - don't use a file left around from a previous run
# TODO - clear temp on errors from updater
try:
with open(self.weatherFile) as file:
return int(file.readline().strip())
except Exception as e:
print(e)
return 0
def getTemperatureCharacters(self, temperature):
return list(str(round(temperature)))

View File

@ -19,19 +19,21 @@ class WeatherUpdater:
self.auth = (user, password)
self.weatherTempFile = 'weather.tmp'
self.weatherFile = 'weather.txt'
# TODO - clear weather file on init and on failure
self.persistTemperature('init')
def updateWeather(self):
try:
temperature = round(self.getTemperature())
self.persistTemperature(temperature)
except requests.exceptions.ConnectionError:
print('connection error', flush=True)
self.characterDisplay.clearRow(self.topRow)
except requests.exceptions.Timeout:
print('timeout', flush=True)
self.characterDisplay.clearRow(self.topRow)
except requests.exceptions.ConnectionError as e:
print(e, flush=True)
self.persistTemperature('error')
except requests.exceptions.Timeout as e:
print(e, flush=True)
self.persistTemperature('error')
def clearWeather(self):
self.persistTemperature('halted')
def getTemperature(self):
weather = self.getWeather()