From 366ebd803bdbf764cde2bd4b9a19fa743e544205 Mon Sep 17 00:00:00 2001 From: zackees Date: Tue, 9 May 2023 20:18:50 -0700 Subject: [PATCH] refactors thread_processor --- src/video_subtitles/gui.py | 40 +---------------------- src/video_subtitles/thread_processor.py | 43 +++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 39 deletions(-) create mode 100644 src/video_subtitles/thread_processor.py diff --git a/src/video_subtitles/gui.py b/src/video_subtitles/gui.py index ec21d62..7a87089 100644 --- a/src/video_subtitles/gui.py +++ b/src/video_subtitles/gui.py @@ -6,10 +6,8 @@ import os import platform -import queue import subprocess import sys -import threading from threading import Thread from PyQt6 import QtCore # type: ignore @@ -27,6 +25,7 @@ from video_subtitles.run import run from video_subtitles.say import say from video_subtitles.settings import Settings +from video_subtitles.thread_processor import ThreadProcessor from video_subtitles.util import MODELS settings = Settings() @@ -147,43 +146,6 @@ def dropEvent(self, event): ) # pass api key to callback -# Can't use futures because it doesn't have a daemon option so we have to -# impliment our own thread processor. -class ThreadProcessor(Thread): - """Thread processor.""" - - def __init__(self) -> None: - super().__init__(daemon=True) - self.pending_tasks: queue.Queue = queue.Queue() - self.processing_task: Thread | None = None - self.event = threading.Event() - self.start() - - def run(self) -> None: - """Process each thread in the queue.""" - while not self.event.wait(0.1): - if self.processing_task is not None: - if not self.processing_task.is_alive(): - self.processing_task.join() - self.processing_task = None - if self.processing_task is not None: - continue - if self.pending_tasks.empty(): - continue - self.processing_task = self.pending_tasks.get() - self.processing_task.start() - - def add(self, thread: Thread) -> None: - """Queues a thread to be executed.""" - assert thread.daemon is True - self.pending_tasks.put(thread) - - def stop(self) -> None: - """Stops the thread executor.""" - self.event.set() - self.join() - - def run_gui() -> None: """Runs the gui.""" app = QApplication(sys.argv) diff --git a/src/video_subtitles/thread_processor.py b/src/video_subtitles/thread_processor.py new file mode 100644 index 0000000..43dbb8d --- /dev/null +++ b/src/video_subtitles/thread_processor.py @@ -0,0 +1,43 @@ +""" +Processes threads one at a time. +""" + +import queue +import threading + + +# Can't use futures because it doesn't have a daemon option so we have to +# impliment our own thread processor. +class ThreadProcessor(threading.Thread): + """Thread processor.""" + + def __init__(self) -> None: + super().__init__(daemon=True) + self.pending_tasks: queue.Queue = queue.Queue() + self.processing_task: threading.Thread | None = None + self.event = threading.Event() + self.start() + + def run(self) -> None: + """Process each thread in the queue.""" + while not self.event.wait(0.1): + if self.processing_task is not None: + if not self.processing_task.is_alive(): + self.processing_task.join() + self.processing_task = None + if self.processing_task is not None: + continue + if self.pending_tasks.empty(): + continue + self.processing_task = self.pending_tasks.get() + self.processing_task.start() + + def add(self, thread: threading.Thread) -> None: + """Queues a thread to be executed.""" + assert thread.daemon is True + self.pending_tasks.put(thread) + + def stop(self) -> None: + """Stops the thread executor.""" + self.event.set() + self.join()