2

How to run Qthread one after another?

For the following code:

import sys
from PyQt5 import QtCore, QtWidgets

class MyWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle('MyWindow')
        self._main = QtWidgets.QWidget()
        self.setCentralWidget(self._main) 
        self.button = QtWidgets.QPushButton('Run')
        self.button.clicked.connect(self.my_method)
        layout = QtWidgets.QGridLayout(self._main)
        layout.addWidget(self.button)
        layout.addWidget(self.button)

    def my_method(self):
        for i in range(0,3):
            self.n = 5
            self.loadthread = MyThread(self.n, self)
            self.loadthread.start()
    
class MyThread(QtCore.QThread):
    def __init__(self, n, parent=None):
        QtCore.QThread.__init__(self, parent)
        self.n = n

    def run(self):
        for i in range(self.n):
            print(i)


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    screen = MyWindow()
    screen.show()
    sys.exit(app.exec_())

I am getting the output as:

0
1
0
2
3
1
2
3
4
4
0
1
2
3
4

The threads are running concurrently. I want it to run one after another.

Desired output:

0
1
2
3
4
0
1
2
3
4
0
1
2
3
4

How can I run the threads only after one is finished?

I tried using thread.wait(). It works. But the UI freezes during that.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241

1 Answers1

0

I saw your task like this:

import sys
from PyQt5 import QtCore, QtWidgets


class MyThread(QtCore.QThread):
    def __init__(self, n, parent=None):
        QtCore.QThread.__init__(self, parent)
        self.n = n

    def run(self):
        for i in range(self.n):
            print(i)
            self.msleep(50)
            

class MyWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle('MyWindow')
        self._main = QtWidgets.QWidget()
        self.setCentralWidget(self._main) 
        
        self.button = QtWidgets.QPushButton('Run')
        self.button.clicked.connect(self.my_method)
        
        layout = QtWidgets.QGridLayout(self._main)
        layout.addWidget(self.button)
        layout.addWidget(self.button)
        
        self.n = 5
        self.loadthread = MyThread(self.n, self)
        self.loadthread.finished.connect(self.task_finished)

    def my_method(self):
        self.button.setEnabled(False)
        self.number_of_tasks = 3
        self.list_tasks = [i for i in range(self.number_of_tasks, 0, -1)]
        self.load_task()
        
    def load_task(self):
        self.number_of_tasks -= 1
        self.task = self.list_tasks[self.number_of_tasks]
        print(f'\n Start task: {self.task}')
        self.loadthread.start()            

    def task_finished(self):
        print(f'--- Finish task: {self.task}')
        if self.number_of_tasks:
            self.load_task()
        else:
            self.button.setEnabled(True)


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    screen = MyWindow()
    screen.show()
    sys.exit(app.exec_())

enter image description here

S. Nick
  • 12,879
  • 8
  • 25
  • 33