I am creating an application that manages data. One function requires a thread to gather data in the background. To do that, I created a variable called bookGrabber
in my main class. At some point, the user starts the function to gather data, which assigns the BookGrabber
class to the variable self.bookGrabber
The BookGrabber class is a metadata check that utilizes a list (data) to check for fields in an entry on a webpage and inserts a dataclass with the data into a database. It is a class that inherits from QThread.
class Ui(Ui_Mainwindow):
def __init__(self, MainWindow, username) -> None:
[...]
self.bookGrabber = None
def btn_add_medium(self):
[...]
self.bookGrabber = BookGrabber(mode = mode, prof_id = prof_id, app_id = app_id, data = data)
self.bookGrabber.finished.connect(self.bookGrabber.deleteLater)
self.bookGrabber.finished.connect(self.hide_progress_label)
self.bookGrabber.finished.connect(self.update_app_media_list)
self.bookGrabber.updateSignal.connect(self.update_progress_label)
self.bookGrabber.start()
while self.bookGrabber.isRunning():
print("waiting for thread to finish")
QtWidgets.QApplication.processEvents()
I am having a problem with the code – the second time it is run, the returned Data contains an error. The error is “Book not present” and is caused by the app_id in the current dataset, that, even though I pass the new app_id into the BookGrabber, uses the older app_id.
This brings me to my question:
How do I effectively kill a thread in python/pyqt to allow the usage of the same variable for multiple threads?
I tried using del self.bookGrabber
after the thread ended, setting self.bookGrabber to None
before and after initializing the thread.
What is the best way to do this?
If anything else is required, I can update the question.
Any help is appreciated, thanks in advance.