PyQt QueuedConnection sometimes works

1.9k views Asked by At

I have an application using QThreads. I am adding different methods to a queued connection and they are run in that sequence. This has been working very well. But the depending on the order of the tests (sequence can be in any order) my application will just stop (It won't freeze, but it won't finish calling the rest of the tests in the queue). For example if running the sequence [test1, test2, test3]. I will be able to run and finish (finish signal is emitted) test1 and test2, but test3 will not be called. However, this only happens sometimes, not all the time. I think the thread is quitting before all the tasks are complete. Is there something I should be clearing?

Relevant code below:

    # Initialize QThread
    self.obj = signalTest.Worker()
    self.thread = QThread()
    self.obj.moveToThread(self.thread)
    self.obj.finished.connect(self.signalFinished)

def runTests(self) # This is triggered by clicking a button
    self.thread.start()
    for index in range(0, len(sequence)):
        if self.queue[index] == 'test1'
            QMetaObject.invokeMethod(self.obj, 'run_test1', Qt.QueuedConnection,
                                     Q_ARG(int, index)
        elif self.queue[index] == 'test2'
            QMetaObject.invokeMethod(self.obj, 'run_test2', Qt.QueuedConnection)
        elif self.queue[index] == 'test3'
            QMetaObject.invokeMethod(self.obj, 'run_test3', Qt.QueuedConnection)

@pyqtSlot(int)
def signalFinished(self, row)
    # Do something here

Worker thread:

class Worker(QObject):
    finished = pyqtSignal(int)

    @pyqtSlot(int)
    def test1(self):
        """
        Runs test1
        """
        if self.abort is False:
            # Run test1 here
        self.finished.emit(row)
....
0

There are 0 answers