PyQt5 - Multithreading

Multithreading

Multithreading technology is used to design three methods, one is to use counter module QTimer, the other is to use multithreading module QThread, and the other is to use event processing function.

QTimer

If you want to perform an operation periodically in the application summary, such as periodically detecting the CPU of the host, you need to use the QTimer (timer). The QTimer class provides repetitive and single timers. To use the timer, you need to first create an instance of the QTImer, connect its timeout signal to the corresponding slot, and call start.

# -*- coding: utf-8 -*-

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys

class WinForm(QWidget):
    def __init__(self, parent = None):
        super(WinForm, self).__init__(parent)
        self.setWindowTitle("QTimer Demo")
        self.listFile =QListWidget()
        self.label = QLabel('Display the current time')
        self.startBtn = QPushButton("start")
        self.endBtn = QPushButton("End")
        layout = QGridLayout(self)
        # Initialize a timer
        self.timer = QTimer(self)
        self.timer.timeout.connect(self.showTime)

        layout.addWidget(self.label,0,0,1,2)
        layout.addWidget(self.startBtn,1,0)
        layout.addWidget(self.endBtn,1,1)

        self.startBtn.clicked.connect(self.startTimer)
        self.endBtn.clicked.connect(self.endTimer)
        self.setLayout(layout)
    def showTime(self):
        time = QDateTime.currentDateTime()
        timeDisplay = time.toString("yyyy-MM-dd hh:mm:ss dddd")
        self.label.setText(timeDisplay)
    def startTimer(self):
        # Set the time interval and start the timer
        self.timer.start(1000)
        self.startBtn.setEnabled(False)
        self.endBtn.setEnabled(True)
    def endTimer(self):
        self.timer.stop()
        self.startBtn.setEnabled(True)
        self.endBtn.setEnabled(False)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    form = WinForm()
    form.show()
    sys.exit(app.exec_())


The demo pops up a window, which disappears after 10 seconds.

# -*- coding: utf-8 -*-

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *

if __name__ == '__main__':
    app = QApplication(sys.argv)
    label = QLabel("<font color=red size=128><b>Hello PyQT, The window disappears after 10</b></font>")
    label.setWindowFlags(Qt.SplashScreen| Qt.FramelessWindowHint)
    label.show()
    QTimer.singleShot(10000, app.quit)
    sys.exit(app.exec_())

QThread

QThread is the core underlying class in Qt threads

Thread instances can be invoked directly when using threads, and threads can be started by calling its start() function. After starting threads, the run method implemented by threads can be invoked automatically. This method starts with the execution function of threads.

The thread task of the business is written in the run function, and the thread ends when run exits. QThread has strarted and finished signals. It can specify slot functions for these two signals, and specify a section of code to initialize and release resources after the start and end of the thread.

Common methods

  • start
  • wait
  • sleep
# -*- coding: utf-8 -*-

from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
import sys

class MainWidget(QWidget):
    def __init__(self, parent =None):
        super(MainWidget, self).__init__(parent)
        self.setWindowTitle("QThread Example")
        self.thread = Worker()
        self.listFile = QListWidget()
        self.btnStart = QPushButton("start")
        layout = QGridLayout(self)
        layout.addWidget(self.listFile,0,0,1,2)
        layout.addWidget(self.btnStart,1,1)
        self.btnStart.clicked.connect(self.slotStart)
        self.thread.sinout.connect(self.slotAdd)
    def slotStart(self):
        self.btnStart.setEnabled(False)
        self.thread.start()

    def slotAdd(self,file_inf):
        self.listFile.addItem(file_inf)

class Worker(QThread):
    sinout=  pyqtSignal(str)
    def __init__(self, parent = None):
        super(Worker,self).__init__(parent)
        self.working = True
        self.num = 0

    def __del__(self):
        self.working = False
        self.wait()

    def run(self):
        while self.working == True:
            file_str = "File index {0} ".format(self.num)
            self.num += 1
            # Transmitting signal
            self.sinout.emit(file_str)
            # Thread hibernates for 2 seconds
            self.sleep(2)
if __name__ == '__main__':
    app = QApplication(sys.argv)
    demo = MainWidget()
    demo.show()
    sys.exit(app.exec_())

Although the interface data display and data read-write are separated, if the data read-write is very time-consuming, the interface will be stuck.

# -*- coding: utf-8 -*-

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *

global sec
sec =0

def setTime():
    global sec
    sec += 1
    lcdNumber.display(sec)
def work():
    timer.start(1000)
    for i in  range(200000000):
        pass
    timer.stop()
if __name__ == "__main__":
    app = QApplication(sys.argv)
    top = QWidget()
    top.resize(300,120)

    layout = QVBoxLayout(top)
    lcdNumber = QLCDNumber()
    layout.addWidget(lcdNumber)
    button = QPushButton("test")
    layout.addWidget(button)
    timer = QTimer()
    timer.timeout.connect(setTime)
    button.clicked.connect(work)
    top.show()
    sys.exit(app.exec_())

We use loops to simulate very time-consuming work. When the test button is clicked, the program interface stops responding directly. It is not updated until the end of the loop, and the timer is always displayed at 0.

All windows in PyQt are in the main thread of the UI, which blocks the UI thread by performing time-consuming operations, thus stopping the window from responding. If the window does not respond for a long time, it will affect the user experience. To avoid this problem, use the QThread to open a new thread to perform time-consuming operations on that thread.

# -*- coding: utf-8 -*-

import sys
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *

global sec
sec = 0
class WorkThread(QThread):
    trigger = pyqtSignal()
    def __init__(self):
        super(WorkThread, self).__init__()
    def run(self):
        for i in range(2000000000):
            pass
        self.trigger.emit()

def countTime():
    global sec
    sec += 1
    lcdNumber.display(sec)

def work():
    timer.start(1000)
    workThread.start()
    workThread.trigger.connect(timeStop)

def timeStop():
    timer.stop()
    print("End-of-run time",lcdNumber.value())
    global sec
    sec = 0


if __name__ == "__main__":
    app = QApplication(sys.argv)
    top = QWidget()
    top.resize(300,120)
    layout = QVBoxLayout(top)
    lcdNumber = QLCDNumber()
    layout.addWidget(lcdNumber)
    button = QPushButton("test")
    layout.addWidget(button)

    timer = QTimer()
    workThread = WorkThread()
    button.clicked.connect(work)
    timer.timeout.connect(countTime)
    top.show()
    sys.exit(app.exec_())

WorkThread inherits from the QThread class and rewrites its run function. The run() function is what the new thread needs to execute. In the run function, a loop is executed, and then the calculated signal is transmitted.

event processing

PyQt uses two mechanisms for event handlers: high-level signal and slot mechanisms and low-level event handlers. We introduce the use of the low-level event handler, the processEvents() function, whose function is to process time, simply to refresh the page.

For a time-consuming program, PyQt has to wait for the program to finish execution before it can proceed to the next step, which is shown as Katon on the page.

# -*- coding: utf-8 -*-

from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import sys
import time

class WinForm(QWidget):
    def __init__(self):
        super(WinForm, self).__init__()
        self.setWindowTitle("Examples of real-time page refresh")
        self.listFile = QListWidget()
        self.btnStart = QPushButton("start")
        layout = QGridLayout(self)
        layout.addWidget(self.listFile,0,0,1,2)
        layout.addWidget(self.btnStart,1,1)
        self.btnStart.clicked.connect(self.slotAdd)
        self.setLayout(layout)
    def slotAdd(self):
        for n in range(10):
            str_n = "File index {0}".format(n)
            self.listFile.addItem(str_n)
            QApplication.processEvents()
            time.sleep(1)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    form = WinForm()
    form.show()
    sys.exit(app.exec_())

Keywords: Qt Windows

Added by victor78 on Thu, 26 Sep 2019 10:33:26 +0300