Having lots of troubles with a multithreaded program in which i am building a gui to automate the processing of neural data by using an API in the backend. I have tried looking at many different resources but I am specifically having trouble with keeping the thread alive when I need to call the same "run_worker" function from the WorkerThread multiple times, since the "run_worker" function will emit a finished signal once it is done. How can I fix this so that the thread can continue after each step of data processing when a button is clicked? Here is my current code for the WorkerThread and UI and an example of a function I need to call when the evee button is clicked in the UI class.
from PyQt5.QtCore import QObject, pyqtSignal as Signal, pyqtSlot as Slot
class WorkerThread(QObject):
finished = Signal()
#progress = Signal()
def __init__(self, parent=None):
super(WorkerThread, self).__init__(parent)
@Slot()
def run_worker(self, process_object, function, *args):
func = getattr(process_object, function)
if func is not None and callable(func):
if args:
func(*args)
#self.finished.emit()
else:
func()
else:
raise ValueError("Provided function is not callable.")
self.finished.emit()
class UI(QMainWindow):
work_requested = Signal()
def __init__(self):
super(UI, self).__init__()
loadUi("processing_gui.ui", self)
self.setFixedWidth(1200)
self.setFixedHeight(1080)
#SETUP WORKER THREAD
self.worker_thread = QThread() #also tried initializing as QThread(parent=self)
self.worker = WorkerThread()
#self.worker.finished.connect(self.worker.deleteLater) #?
self.work_requested.connect(self.worker.run_worker)
self.worker_thread.start()
# show the window
self.show()
self.timeseries_start = False
self.lr_complete = False
self.preprocess_complete = False
self.evee.clicked.connect(self.on_preprocess_click) #preprocess, bandpass filter, mean projection calculation, and motion correction
def start(self, process_object, function, *args):
self.work_requested.emit(process_object, function, *args)
def on_preprocess_click(self):
if self.timeseries_start:
try:
self.show_complete_dialog('Preprocessing has begun!')
self.evee.setEnabled(False)
self.start(self.timeseries_process_object, "preprocess", 2, 4)
except:
self.show_error_message(
'Preprocessing could not be completed because -PP files already exist. Recheck file names or delete the files ending in -PP from processed directory to try again.')
return
try:
self.start(self.timeseries_process_object, "bandpass_filter")
except:
self.show_error_message(
'Bandpass filtering could not be completed because the -PP-BP files already exist. Delete them from the processed directory to continue.'
)
return
try:
self.start(self.timeseries_process_object, "mean_projection_frame")
except:
self.show_error_message('Mean projection frame could not be calculated because it was already calculated. Delete -mean_image.isxd files form processed folder to continue')
return
try:
self.start(self.timeseries_process_object, "motion_correction")
except:
self.show_error_message(
'Motion correction could not be completed because the -PP-BP-MC files already exist. Delete them from the processed directory to continue'
)
return
self.show_complete_dialog('Proprocessing has been completed!')
self.preprocess_complete = True
self.evee.setEnabled(True)
I have tried using a QQueue in the worker thread but continued to get import errors despite trying to upgrade and reinstall in my virtual environment. I would appreciate any insight as to how I can keep my thread running as I call the same "run_worker" function dynamically multiple times within my on click functions. The functions I am running in the backend take a significant amount of time (about 40 mins for some buttons) so my GUI continues to be unresponsive and gives me the Qthread destroyed error while its running.
timeseries_process_object
functions they're getting withgetattr
, but they didn't keep a persistent reference to it, which is what normally causes that error.