How to 'kill' a QThreadPool process?
-
I am currently having using the pyside bult in QThreadPool class to launch threads in my application. Upon looking around in the documentation, I am having an issue trying to find a way to 'kill' a process.
I need to have this capability since one of the threads that I am launching has matplotlib open, and it stays that way after that code runs which messes with the program.
The solution seems to be that I need to kill that process, but I am not sure how to do this with QThreadPool.
Does QThreadPool have a method of doing this or Python Qt in general? Or should I attempt to do this another way?
-
@ccortez You need QThread instance you want to kill then you can call https://doc.qt.io/qt-6/qthread.html#terminate
-
@jsulm Thank you! This some what helped, but I am getting a freezing GUI, which I think defeats the point of doing the multithreading. Could you possibly shine light on why this may be happening if you can?
Here is a sample of my code if it helps:
class Worker(QObject): finished = Signal(str) progress = Signal(int) def run(self, file): """Long-running task." that calls a separate class for computation"" b = SeparateClass() b.doComputation() self.finished.emit() class DataPlotting(QMainWindow): def __init__(self): self.thread = QThread() self.worker = Worker() self.report_builder = QPushButton('Call class that threads') self.report_builder.setEnabled(False) self.report_builder.clicked.connect(self.qthread_test) @Slot() def qthread_test(self): file = self.file_dropdown.currentText() self.worker.moveToThread(self.thread) self.thread.started.connect(self.worker.run(file)) self.worker.finished.connect(self.thread.quit) self.worker.finished.connect(self.worker.deleteLater) self.thread.finished.connect(self.thread.deleteLater) self.thread.start() return
This does achieve what I wanted of launching a separate thread and getting rid of it , but the GUI still freezes up
-
@jsulm My UI freezes when I press the button that goes to 'qthread_test' in that code snippet.
It starts to buffer and becomes unresponsive for a bit. That is the code sample that caused it. Yes there rare other elemetns in my code, but they do not connect ao any parts of this, it is mostly other elements like buttons. variables, etc.