A Minimal QThreadPool
Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import sys
from PySide6.QtCore import (QObject, QRunnable,
QThreadPool, Slot, Signal, Qt)
from PySide6.QtWidgets import (QApplication,
QPushButton, QLabel, QWidget, QVBoxLayout)
class Signals(QObject):
progress = Signal(str)
error = Signal(str)
# 1. Create a QRunnable subclass
# and implement its run() method.
class Runnable(QRunnable):
signals = Signals()
def __init__(self, parent=None):
super().__init__(parent)
# The run method will be executed
# in the worker thread.
def run(self):
self.signals.progress.emit('Progress emitted')
print('Hello World')
class Window(QWidget):
def __init__(self):
super().__init__()
layout = QVBoxLayout()
self.setLayout(layout)
button = QPushButton('Start background thread')
button.clicked.connect(self.on_button_clicked)
self.label = QLabel()
self.label.setAlignment(Qt.AlignmentFlag.AlignCenter)
layout.addWidget(button)
layout.addWidget(self.label)
# When the button is clicked:
@Slot()
def on_button_clicked(self):
# 2. Create a Runnable object
runnable = Runnable()
runnable.signals.progress.connect(self.label.setText)
runnable.signals.error.connect(self.on_error)
# 3. Access the QThreadPool global instance
# and run the task.
QThreadPool.globalInstance().start(runnable)
@Slot()
def on_error(self, message):
print(message)
if __name__ == '__main__':
app = QApplication(sys.argv)
main_window = Window()
main_window.show()
sys.exit(app.exec())
The QThreadPool
class manages a collection of QThread
s and is used together with the QRunnable
class. To use it in your application
-
Create a
QRunnable
subclass and implement itsrun()
method to contain the code you want to execute in a background thread.QRunnable
itself is not a subclass ofQObject
so you can not add signals to it directly but you can easily work around this limitation by creating a separateQObject
subclass (namedSignals
in the example) and adding an object of that class to yourQRunnable
. In the example therun()
methods emits theSignals.progress
signal and prints a message in the terminal. -
In the main window, create a
Runnable
instance -
Use
QThreadPool.globalInstance()
to get a reference to the global thread pool instance andQThreadPool.start()
to execute theRunnable.run()
method in one of the threads the global thread pool manages.