mirror of
https://github.com/ynput/ayon-core.git
synced 2025-12-24 21:04:40 +01:00
moved idle thread to threads file
This commit is contained in:
parent
4c28395143
commit
6908ae0e4e
2 changed files with 79 additions and 78 deletions
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue