From 5f39139b7c26750d99d27a555b0262b209212b7c Mon Sep 17 00:00:00 2001 From: petrucci4prez Date: Sat, 10 Jun 2017 01:52:06 -0400 Subject: [PATCH] explicitly stop statemachine threads --- blinkenLights.py | 5 +++-- listeners.py | 26 +++++++++++++++++--------- soundLib.py | 11 +++++++---- stateMachine.py | 5 +++++ 4 files changed, 32 insertions(+), 15 deletions(-) diff --git a/blinkenLights.py b/blinkenLights.py index f3fb799..9953347 100644 --- a/blinkenLights.py +++ b/blinkenLights.py @@ -37,8 +37,9 @@ class Blinkenlights(ExceptionThread): logger.debug('Starting LED on pin %s', self._pin) def stop(self): - self._stopper.set() - logger.debug('Stopping LED on pin %s', self._pin) + if self.is_alive(): + self._stopper.set() + logger.debug('Stopping LED on pin %s', self._pin) def setCyclePeriod(self, cyclePeriod): self._sleeptime = cyclePeriod/20/2 diff --git a/listeners.py b/listeners.py index cd8432b..a4c8f62 100644 --- a/listeners.py +++ b/listeners.py @@ -118,6 +118,16 @@ class KeypadListener: self._listener.start() logger.debug('Started keypad listener') + def stop(self): + try: + self._dev.ungrab() + self._dev = None + logger.debug('Released keypad device') + except IOError: + logger.error('Failed to release keypad device') + except AttributeError: + pass + def resetBuffer(self): self._stopResetCountdown self._clearBuffer() @@ -134,13 +144,7 @@ class KeypadListener: self._buf = '' def __del__(self): - try: - self._dev.ungrab() - logger.debug('Released keypad device') - except IOError: - logger.error('Failed to release keypad device') - except AttributeError: - pass + self.stop() class PipeListener(ExceptionThread): ''' @@ -177,9 +181,13 @@ class PipeListener(ExceptionThread): ExceptionThread.start(self) logger.debug('Started pipe listener at path %s', self._path) - def __del__(self): + def stop(self): try: os.remove(self._path) + logger.debug('Cleaned up pipe listener at path %s', self._path) except FileNotFoundError: pass - logger.debug('Cleaned up pipe listener at path %s', self._path) + + def __del__(self): + self.stop() + diff --git a/soundLib.py b/soundLib.py index e083610..923dd0f 100644 --- a/soundLib.py +++ b/soundLib.py @@ -91,6 +91,12 @@ class SoundLib: def start(self): self._startMonitor() + + def stop(self): + self._stopMonitor() + self._ttsSounds.clear() + # this sometimes casues "Fatal Python error: (pygame parachute) Segmentation Fault" + mixer.quit() def changeVolume(self, volumeDelta): newVolume = self.volume + volumeDelta @@ -194,7 +200,4 @@ class SoundLib: logger.debug('Stopping TTS Queue Monitor') def __del__(self): - self._stopMonitor() - self._ttsSounds.clear() - # this sometimes casues "Fatal Python error: (pygame parachute) Segmentation Fault" - mixer.quit() + self.stop() diff --git a/stateMachine.py b/stateMachine.py index 98efe30..4d7c1f3 100644 --- a/stateMachine.py +++ b/stateMachine.py @@ -204,6 +204,11 @@ class StateMachine: self.currentState.entry() def __exit__(self, exception_type, exception_value, traceback): + for i in ['LED', 'camera', 'fileDump', 'soundLib', 'secretListener', 'keypadListener']: + try: + getattr(self, i).stop() + except AttributeError: + pass for i in ['LED', 'camera', 'fileDump', 'soundLib', 'secretListener', 'keypadListener']: try: getattr(self, i).__del__()