From 8dc0fd6a151e191381085b6dd72f484c49eacdbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=85ke=20Forslund?= Date: Sat, 3 Jul 2021 15:58:27 +0200 Subject: [PATCH] Make _paused state an event This removes the need for a busy waiting while / sleep check, instead uses the Event.wait() to wait until playback is resumed. --- mycroft/tts/tts.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/mycroft/tts/tts.py b/mycroft/tts/tts.py index 408816cc18e..9fa969ae729 100644 --- a/mycroft/tts/tts.py +++ b/mycroft/tts/tts.py @@ -17,7 +17,7 @@ import random import re from abc import ABCMeta, abstractmethod -from threading import Thread +from threading import Event, Thread from time import time, sleep from warnings import warn @@ -54,7 +54,10 @@ class PlaybackThread(Thread): def __init__(self, queue): super(PlaybackThread, self).__init__() self.queue = queue - self._paused = False + + self._free_to_play = Event() + self._free_to_play.set() + self._terminated = False self._processing_queue = False self.enclosure = None @@ -94,8 +97,7 @@ def run(self): listening. """ while not self._terminated: - while self._paused: - sleep(0.2) + self._free_to_play.wait() try: (snd_type, data, visemes, ident, listen) = self.queue.get(timeout=2) @@ -116,7 +118,7 @@ def run(self): if self.p: while self.p.poll() is None: - if self._paused: + if not self._free_to_play.is_set(): self.p.terminate() break sleep(0.1) @@ -157,12 +159,12 @@ def blink(self, rate=1.0): self.enclosure.eyes_blink("b") def pause(self): - """pause thread""" - self._paused = True + """Signal that the thread is no longer free to play.""" + self._free_to_play.clear() def resume(self): - """resume thread""" - self._paused = False + """Signal that the thred is now free to play.""" + self._free_to_play.set() def stop(self): """Stop thread"""