Skip to content

Commit

Permalink
refactors thread_processor
Browse files Browse the repository at this point in the history
  • Loading branch information
zackees committed May 10, 2023
1 parent 4e15c05 commit 366ebd8
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 39 deletions.
40 changes: 1 addition & 39 deletions src/video_subtitles/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -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)
Expand Down
43 changes: 43 additions & 0 deletions src/video_subtitles/thread_processor.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 366ebd8

Please sign in to comment.