From 6908ae0e4e14720d736f8ce51e8eb20b33a239eb Mon Sep 17 00:00:00 2001 From: iLLiCiTiT Date: Fri, 19 Mar 2021 15:01:00 +0100 Subject: [PATCH] moved idle thread to threads file --- pype/modules/idle_manager/idle_module.py | 84 ++--------------------- pype/modules/idle_manager/idle_threads.py | 73 ++++++++++++++++++++ 2 files changed, 79 insertions(+), 78 deletions(-) diff --git a/pype/modules/idle_manager/idle_module.py b/pype/modules/idle_manager/idle_module.py index 25309e9443..979e1b92ea 100644 --- a/pype/modules/idle_manager/idle_module.py +++ b/pype/modules/idle_manager/idle_module.py @@ -1,11 +1,8 @@ -import time import collections -import threading from abc import ABCMeta, abstractmethod import six -from pype.lib import PypeLogger from pype.modules import PypeModule, ITrayService @@ -79,11 +76,16 @@ class IdleManager(PypeModule, ITrayService): if self.idle_thread and self.idle_thread.is_running: return self.idle_thread.idle_time + def _create_thread(self): + from .idle_threads import IdleManagerThread + + return IdleManagerThread(self) + def start_thread(self): if self.idle_thread: self.idle_thread.stop() self.idle_thread.join() - self.idle_thread = IdleManagerThread(self) + self.idle_thread = self._create_thread() self.idle_thread.start() def stop_thread(self): @@ -93,77 +95,3 @@ class IdleManager(PypeModule, ITrayService): def on_thread_stop(self): self.set_service_failed_icon() - - -class IdleManagerThread(threading.Thread): - def __init__(self, module, *args, **kwargs): - super(IdleManagerThread, self).__init__(*args, **kwargs) - self.log = PypeLogger.get_logger(self.__class__.__name__) - self.module = module - self.threads = [] - self.is_running = False - self.idle_time = 0 - - def stop(self): - self.is_running = False - - def reset_time(self): - self.idle_time = 0 - - @property - def time_callbacks(self): - return self.module.time_callbacks - - def on_stop(self): - self.is_running = False - self.log.info("IdleManagerThread has stopped") - self.module.on_thread_stop() - - def _create_threads(self): - from .idle_logic import MouseThread, KeyboardThread - - thread_mouse = MouseThread(self.reset_time) - thread_keyboard = KeyboardThread(self.reset_time) - return thread_mouse, thread_keyboard - - def run(self): - self.log.info("IdleManagerThread has started") - self.is_running = True - thread_mouse, thread_keyboard = self._create_threads() - thread_mouse.start() - thread_keyboard.start() - try: - while self.is_running: - if self.idle_time in self.time_callbacks: - for callback in self.time_callbacks[self.idle_time]: - thread = threading.Thread(target=callback) - thread.start() - self.threads.append(thread) - - for thread in tuple(self.threads): - if not thread.isAlive(): - thread.join() - self.threads.remove(thread) - - self.idle_time += 1 - time.sleep(1) - - except Exception: - self.log.warning( - 'Idle Manager service has failed', exc_info=True - ) - - # Threads don't have their attrs when Qt application already finished - try: - thread_mouse.stop() - thread_mouse.join() - except AttributeError: - pass - - try: - thread_keyboard.stop() - thread_keyboard.join() - except AttributeError: - pass - - self.on_stop() diff --git a/pype/modules/idle_manager/idle_threads.py b/pype/modules/idle_manager/idle_threads.py index ab3f6790e6..7cedf986e6 100644 --- a/pype/modules/idle_manager/idle_threads.py +++ b/pype/modules/idle_manager/idle_threads.py @@ -1,5 +1,10 @@ +import time +import threading + from pynput import mouse, keyboard +from pype.lib import PypeLogger + class MouseThread(mouse.Listener): """Listens user's mouse movement.""" @@ -22,3 +27,71 @@ class KeyboardThread(keyboard.Listener): def on_press(self, key): self.callback() + + +class IdleManagerThread(threading.Thread): + def __init__(self, module, *args, **kwargs): + super(IdleManagerThread, self).__init__(*args, **kwargs) + self.log = PypeLogger.get_logger(self.__class__.__name__) + self.module = module + self.threads = [] + self.is_running = False + self.idle_time = 0 + + def stop(self): + self.is_running = False + + def reset_time(self): + self.idle_time = 0 + + @property + def time_callbacks(self): + return self.module.time_callbacks + + def on_stop(self): + self.is_running = False + self.log.info("IdleManagerThread has stopped") + self.module.on_thread_stop() + + def run(self): + self.log.info("IdleManagerThread has started") + self.is_running = True + thread_mouse = MouseThread(self.reset_time) + thread_keyboard = KeyboardThread(self.reset_time) + thread_mouse.start() + thread_keyboard.start() + try: + while self.is_running: + if self.idle_time in self.time_callbacks: + for callback in self.time_callbacks[self.idle_time]: + thread = threading.Thread(target=callback) + thread.start() + self.threads.append(thread) + + for thread in tuple(self.threads): + if not thread.isAlive(): + thread.join() + self.threads.remove(thread) + + self.idle_time += 1 + time.sleep(1) + + except Exception: + self.log.warning( + 'Idle Manager service has failed', exc_info=True + ) + + # Threads don't have their attrs when Qt application already finished + try: + thread_mouse.stop() + thread_mouse.join() + except AttributeError: + pass + + try: + thread_keyboard.stop() + thread_keyboard.join() + except AttributeError: + pass + + self.on_stop()