From e6945e6770219daa50fc6b650cf6efeade63d19b Mon Sep 17 00:00:00 2001 From: wesley Date: Mon, 3 May 2021 07:40:49 -0300 Subject: [PATCH] Make play_once and play_many be just one function Before when many playbacks were needed, play_many was executed in its own thread and created a thread for play_once. But now just a "play_once" thread is created and it calls a playback how many times are needed --- atbswp/control.py | 51 +++++++++++++++++++---------------------------- 1 file changed, 20 insertions(+), 31 deletions(-) diff --git a/atbswp/control.py b/atbswp/control.py index eb50d50..0dbef5c 100644 --- a/atbswp/control.py +++ b/atbswp/control.py @@ -354,30 +354,23 @@ class PlayControl: def __init__(self, count=1, infinite=False): self.count = count self.infinite = infinite - self._stop_locks = [True, True] # [once, many] + self._stop_locks = [True] self._current_count = 0 - self._play_once_thread = None - self._play_many_thread = None + self._play_thread = None - def _play_once(self, capture, append_function=None): + def _play(self, capture, append_function=None): for line in capture: - if self._stop_locks[0]: break + if self.is_stoped(): break exec(line) - self._stop_locks[0] = True - if append_function: - append_function() - - def _play_many(self, capture, append_function=None): - self.current_count = 0 - while (self.current_count < self.count or self.infinite)\ - and not self._stop_locks[1]: - self._stop_locks[0] = False - self._play_once_thread = self._start_thread(self._play_once, capture) - self._play_once_thread.join() - self.current_count += 1 - self._stop_locks = [True, True] - if append_function: - append_function() + self._current_count += 1 + print('starting again') + if self._current_count < self.count or self.infinite \ + and not self.is_stoped(): + self._play(capture, append_function) + else: + self.stop() + if append_function: + append_function() def _start_thread(self, target, *args, daemon=False): thread = Thread(target=target, args=(*args,)) @@ -392,10 +385,10 @@ def set_config(self, count, infinite): def get_current_count(self): """Returns the current playback time""" - return current_count + return self._current_count def is_stoped(self): - return self._stop_locks[0] and self._stop_locks[1] + return self._stop_locks[0] def play(self, capture, append_function=None): """Starts playback according with configs setted @@ -404,17 +397,13 @@ def play(self, capture, append_function=None): :param append_function: a function to be executed after a playback """ - if self.infinite or self.count > 1: - self._stop_locks = [False, False] - self._start_thread(self._play_many, capture, - append_function) - else: - self._stop_locks[0] = False - self._start_thread(self._play_once, capture, - append_function) + self._stop_locks[0] = False + self._current_count = 0 + self._play_thread = self._start_thread(self._play, capture, + append_function) def stop(self): - self._stop_locks = [True, True] + self._stop_locks[0] = True class PlayInterface: