Move worker classes to thread_my_stuff

This commit is contained in:
Kameron Kenny 2024-09-17 15:18:48 -04:00
parent eb9b007703
commit 9d6053403a
No known key found for this signature in database
GPG Key ID: E5006629839D2276
2 changed files with 79 additions and 73 deletions

View File

@ -1,9 +1,8 @@
#!/usr/bin/env python
import os
import sys
import traceback
from PyQt6.QtCore import pyqtSignal, pyqtSlot, QRunnable, QObject, QThreadPool
from PyQt6.QtCore import QThreadPool
from PyQt6.QtWidgets import QMainWindow, QApplication
from PyQt6.QtGui import QIcon,QPixmap
@ -16,76 +15,10 @@ from get_image_tag import get_exif_tag
from media import Media
from lumberjack import timber
from raw_photo import extract_jpg_thumb, get_raw_image_dimensions
from thread_my_stuff import Worker
log = timber(__name__)
class WorkerSignals(QObject):
"""
Defines the signals avail from a running worker thread.
Supported signals are:
finished
No Data
error
tuple (exctype, value, traceback.format_exc() )
result
object data returned from processing, anything
progress
int indicating % progress
"""
started = pyqtSignal()
finished = pyqtSignal()
error = pyqtSignal(tuple)
result = pyqtSignal(object)
progress = pyqtSignal(int)
class Worker(QRunnable):
"""
Worker Thread
Inherits from QRunnable to handler worker thread setup, signals and wrap-up.
:param callback: The function callback to run on this worker thread. Supplied args and
kwargs will be passed through to the runner.
:type callback: function
:param args: Arguments to pass to the callback function
:param kwargs: Keywords to pass to the callback function
"""
def __init__(self,fn,*args,**kwargs):
super(Worker,self).__init__()
# Store constructor args (re-used for processing)
self.fn = fn
self.args = args
self.kwargs = kwargs
self.signals = WorkerSignals()
# Add the callback to our kwargs
self.kwargs['progress_callback'] = self.signals.progress
@pyqtSlot()
def run(self):
"""
Initialise the runner function with passed args, kwargs.
"""
# Retrieve args/kwargs here; and fire processing using them
try:
self.signals.started.emit()
result = self.fn(*self.args, **self.kwargs)
except:
traceback.print_exc()
exctype, value = sys.exc_info()[:2]
self.signals.error.emit((exctype,value,traceback.format_exc()))
else:
self.signals.result.emit(result)
finally:
self.signals.finished.emit()
# Subclass QMainWindow to customize your application's main window
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self, *args, **kwargs):
@ -268,7 +201,8 @@ class MainWindow(QMainWindow, Ui_MainWindow):
progress_callback.emit((file_count / self.file_total) * 100)
return "Done."
def print_output(self,s):
@staticmethod
def print_output(s):
print(s)
def scan_thread_started(self):
@ -279,18 +213,17 @@ class MainWindow(QMainWindow, Ui_MainWindow):
print('scan thread complete.')
self.toggle_scan_button(True)
def thread_complete(self):
@staticmethod
def thread_complete():
print("THREAD COMPLETE.")
def find_files(self):
""" find files to build a dictionary out of """
worker = Worker(self.t_find_files)
# worker.signals.started.connect(self.toggle_scan_button(enable=False))
worker.signals.started.connect(self.scan_thread_started)
worker.signals.result.connect(self.print_output)
worker.signals.finished.connect(self.thread_complete)
worker.signals.finished.connect(self.scan_thread_done)
# worker.signals.finished.connect(self.toggle_scan_button(enable=True))
worker.signals.progress.connect(self.progress_fn)
# Execute.

View File

@ -0,0 +1,73 @@
#!/usr/bin/env python
import traceback
from PyQt6.QtCore import pyqtSignal, pyqtSlot, QRunnable, QObject
class WorkerSignals(QObject):
"""
Defines the signals avail from a running worker thread.
Supported signals are:
finished
No Data
error
tuple (exctype, value, traceback.format_exc() )
result
object data returned from processing, anything
progress
int indicating % progress
"""
started = pyqtSignal()
finished = pyqtSignal()
error = pyqtSignal(tuple)
result = pyqtSignal(object)
progress = pyqtSignal(int)
class Worker(QRunnable):
"""
Worker Thread
Inherits from QRunnable to handler worker thread setup, signals and wrap-up.
:param callback: The function callback to run on this worker thread. Supplied args and
kwargs will be passed through to the runner.
:type callback: function
:param args: Arguments to pass to the callback function
:param kwargs: Keywords to pass to the callback function
"""
def __init__(self,fn,*args,**kwargs):
super(Worker,self).__init__()
# Store constructor args (re-used for processing)
self.fn = fn
self.args = args
self.kwargs = kwargs
self.signals = WorkerSignals()
# Add the callback to our kwargs
self.kwargs['progress_callback'] = self.signals.progress
@pyqtSlot()
def run(self):
"""
Initialise the runner function with passed args, kwargs.
"""
# Retrieve args/kwargs here; and fire processing using them
try:
self.signals.started.emit()
result = self.fn(*self.args, **self.kwargs)
except:
traceback.print_exc()
exctype, value = sys.exc_info()[:2]
self.signals.error.emit((exctype,value,traceback.format_exc()))
else:
self.signals.result.emit(result)
finally:
self.signals.finished.emit()