Python - PyQt : Continue after a QThread is finished - python

I have a for loop in a QThread, which is started from the main GUI via a push button. When the for loop is over I would like to come back to the main thread (which is inside the Gui class) and do something else.
As far as I understood, one should use the join method to wait for the thread to be finished. In my case it seems that MyThread is never finished.
import sys
from PyQt5 import QtCore
import PyQt5.QtWidgets as QtW
from PyQt5.QtCore import QThread
class MyWindow(QtW.QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle('MyWindow')
self._main = QtW.QWidget()
self.setCentralWidget(self._main)
self.button = QtW.QPushButton('Do it', self)
self.button.clicked.connect(self.MyMethod)
self.layout = QtW.QGridLayout(self)
self.layout.addWidget(self.button)
self.setLayout(self.layout)
def MyMethod(self):
self.n = 5
self.loadthread = MyThread(self.n)
self.loadthread.start()
self.loadthread.join() # Will wait for the thread until it finishes its task
print('thread finished')
class MyThread(QThread):
def __init__(self, n):
QThread.__init__(self)
self.n = n
def run(self):
for i in range(self.n):
print(i)
print('cycle finished')
if __name__ == '__main__':
app = QtCore.QCoreApplication.instance() # checks if QApplication already exists
if app is None: # create QApplication if it doesnt exist
app = QtW.QApplication(sys.argv)
mainGui = MyWindow()
mainGui.show()
app.aboutToQuit.connect(app.deleteLater)
app.exec_()
The output of the cose is
0
1
2
3
4
cycle finished
and print('thread finished') is never reached.

QThread does not have the join() method, so your application should quit unexpectedly and point to the following error message.
QLayout: Attempting to add QLayout "" to MyWindow "", which already has a layout
QWidget::setLayout: Attempting to set QLayout "" on MyWindow "", which already has a layout
0
1
2
3
Traceback (most recent call last):
File "main.py", line 24, in MyMethod
self.loadthread.join() # Will wait for the thread until it finishes its task
AttributeError: 'MyThread' object has no attribute 'join'
4
cycle finished
Aborted (core dumped)
If you want to execute some task after it is finished executing the thread you must use the finished signal of QThread:
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('Do it')
self.button.clicked.connect(self.my_method)
layout = QtWidgets.QGridLayout(self._main)
layout.addWidget(self.button)
layout.addWidget(self.button)
#QtCore.pyqtSlot()
def my_method(self):
self.n = 5
self.loadthread = MyThread(self.n, self)
self.loadthread.finished.connect(self.on_finished)
self.loadthread.start()
#QtCore.pyqtSlot()
def on_finished(self):
print('thread finished')
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)
print('cycle finished')
if __name__ == '__main__':
app = QtWidgets.QApplication.instance()
if app is None:
app = QtWidgets.QApplication(sys.argv)
mainGui = MyWindow()
mainGui.show()
app.aboutToQuit.connect(app.deleteLater)
sys.exit(app.exec_())

Related

Pyqt5 crashing program with QThread function [duplicate]

I want to use PySide2 Qtcore.Qthread because of Qtcore.Signal, but I end up with this error:
Process finished with exit code -1073740791
from PySide2.QtCore import QThread
class Thread(QThread):
def run(self):
print('task started')
k = 0
for i in range(10000):
for j in range(5000):
k += 1
print('task finished')
Thread().start()
expect to have those prints but I have this error:
Process finished with exit code -1073740791
Update:
so, why this code also throw the same error?
class Thread(QThread):
done = Signal()
def __init__(self):
super(Thread, self).__init__()
def run(self):
print('task started')
k = 0
for i in range(10000):
for j in range(5000):
k += 1
print('task finished')
self.done.emit()
class Widget(QtWidgets.QWidget):
def __init__(self):
super(Widget, self).__init__()
btn = QtWidgets.QPushButton('test', parent=self)
btn.clicked.connect(self.clicked)
btn.show()
def clicked(self):
t = Thread()
t.done.connect(self.done)
t.start()
def done(self):
print('done')
app = QtWidgets.QApplication()
window = Widget()
window.show()
sys.exit(app.exec_())
Explanation
If you run your code in a CMD/Terminal you will get the following error:
QThread: Destroyed while thread is still running
Aborted (core dumped)
And the error is caused because the thread is destroyed while it is still running since it is a local variable, on the other hand QThread needs an event loop to run
Solution
import sys
from PySide2.QtCore import QCoreApplication, QThread
class Thread(QThread):
def run(self):
print("task started")
k = 0
for i in range(10000):
for j in range(5000):
k += 1
print("task finished")
if __name__ == "__main__":
# create event loop
app = QCoreApplication(sys.argv)
th = Thread()
th.start()
th.finished.connect(QCoreApplication.quit)
sys.exit(app.exec_())
Update:
"t" is a local variable that will be eliminated after executing clicked causing the same problem as your initial code, the solution is to prevent it from being destroyed instantly and for this there are 2 options:
Make a "t" class attribute
def clicked(self):
self.t = Thread()
self.t.done.connect(self.done)
self.t.start()
Store the QThread in a container that has a longer life cycle:
class Widget(QtWidgets.QWidget):
def __init__(self):
super(Widget, self).__init__()
btn = QtWidgets.QPushButton('test', parent=self)
btn.clicked.connect(self.clicked)
self.container = []
def clicked(self):
t = Thread()
t.done.connect(self.done)
t.start()
self.container.append(t)
# ...
Pass it as a parent to "self" but for this it is necessary that Thread allow to receive so you must implement that in the constructor:
class Thread(QThread):
done = Signal()
def __init__(self, parent=None):
super(Thread, self).__init__(parent)
# ...
def clicked(self):
t = Thread(self)
t.done.connect(self.done)
t.start()
I found the solution but I don't know why I should do this. the thread should a part of the class.
self.t = Thread()
self.t.done.connect(self.done)
self.t.start()

PySide2 Qthread crash

I want to use PySide2 Qtcore.Qthread because of Qtcore.Signal, but I end up with this error:
Process finished with exit code -1073740791
from PySide2.QtCore import QThread
class Thread(QThread):
def run(self):
print('task started')
k = 0
for i in range(10000):
for j in range(5000):
k += 1
print('task finished')
Thread().start()
expect to have those prints but I have this error:
Process finished with exit code -1073740791
Update:
so, why this code also throw the same error?
class Thread(QThread):
done = Signal()
def __init__(self):
super(Thread, self).__init__()
def run(self):
print('task started')
k = 0
for i in range(10000):
for j in range(5000):
k += 1
print('task finished')
self.done.emit()
class Widget(QtWidgets.QWidget):
def __init__(self):
super(Widget, self).__init__()
btn = QtWidgets.QPushButton('test', parent=self)
btn.clicked.connect(self.clicked)
btn.show()
def clicked(self):
t = Thread()
t.done.connect(self.done)
t.start()
def done(self):
print('done')
app = QtWidgets.QApplication()
window = Widget()
window.show()
sys.exit(app.exec_())
Explanation
If you run your code in a CMD/Terminal you will get the following error:
QThread: Destroyed while thread is still running
Aborted (core dumped)
And the error is caused because the thread is destroyed while it is still running since it is a local variable, on the other hand QThread needs an event loop to run
Solution
import sys
from PySide2.QtCore import QCoreApplication, QThread
class Thread(QThread):
def run(self):
print("task started")
k = 0
for i in range(10000):
for j in range(5000):
k += 1
print("task finished")
if __name__ == "__main__":
# create event loop
app = QCoreApplication(sys.argv)
th = Thread()
th.start()
th.finished.connect(QCoreApplication.quit)
sys.exit(app.exec_())
Update:
"t" is a local variable that will be eliminated after executing clicked causing the same problem as your initial code, the solution is to prevent it from being destroyed instantly and for this there are 2 options:
Make a "t" class attribute
def clicked(self):
self.t = Thread()
self.t.done.connect(self.done)
self.t.start()
Store the QThread in a container that has a longer life cycle:
class Widget(QtWidgets.QWidget):
def __init__(self):
super(Widget, self).__init__()
btn = QtWidgets.QPushButton('test', parent=self)
btn.clicked.connect(self.clicked)
self.container = []
def clicked(self):
t = Thread()
t.done.connect(self.done)
t.start()
self.container.append(t)
# ...
Pass it as a parent to "self" but for this it is necessary that Thread allow to receive so you must implement that in the constructor:
class Thread(QThread):
done = Signal()
def __init__(self, parent=None):
super(Thread, self).__init__(parent)
# ...
def clicked(self):
t = Thread(self)
t.done.connect(self.done)
t.start()
I found the solution but I don't know why I should do this. the thread should a part of the class.
self.t = Thread()
self.t.done.connect(self.done)
self.t.start()

PyQt5 QThread Issue

I am trying to get the basics of QT5 threading. This is my first attempt, combined from various sources:
import sys
from time import sleep
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QGridLayout
from PyQt5.QtCore import QThread, QObject
'''
Traceback (most recent call last):
File "threads.py", line 68, in <module>
main(sys.argv)
File "threads.py", line 63, in main
window = Window()
File "threads.py", line 15, in __init__
self.initUi()
File "threads.py", line 28, in initUi
self.worker.moveToThread(self.thread)
AttributeError: 'NoneType' object has no attribute 'moveToThread'
Press any key to continue . . .
'''
class Window(QWidget):
def __init__(self):
super().__init__()
self.initUi()
self.low = 0
self.high = 100
self.show()
def initUi(self):
self.thread = QThread()
self.worker = Worker(self)
self.worker.moveToThread(self.thread)
self.thread.start()
self.button = QPushButton(
'Start long running task')
self.layout = QGridLayout()
self.layout.addWidget(self.button, 0, 0)
self.setLayout(self.layout)
def Worker(QObject):
def __init__(self, parent):
super(Worker, self).__init__(parent)
do_work()
def do_work(self):
for _ in range(20):
print('running . . .')
sleep(2)
def main(args):
app = QApplication(args)
window = Window()
sys.exit(app.exec_())
if __name__ == '__main__':
main(sys.argv)
I have included the error I get in the code snippet.
From online articles i learned that in PyQt5 I shouldn't subclass QThread.
You have 2 problems, the first is that the worker must be a class for it changes:
def Worker(QObject):
to
class Worker(QObject):
The other problem is that you must call do_work by means of the instance, ie self, for it changes:
do_work()
to:
self.do_work()
In the following part I show a complete example:
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QGridLayout
from PyQt5.QtCore import QThread, QObject
class Window(QWidget):
def __init__(self):
super().__init__()
self.initUi()
self.low = 0
self.high = 100
self.show()
def initUi(self):
self.thread = QThread()
self.worker = Worker()
self.worker.moveToThread(self.thread)
self.thread.started.connect(self.worker.do_work)
self.thread.finished.connect(self.thread.deleteLater)
self.button = QPushButton(
'Start long running task')
self.button.clicked.connect(self.thread.start)
self.layout = QGridLayout()
self.layout.addWidget(self.button, 0, 0)
self.setLayout(self.layout)
class Worker(QObject):
def __init__(self, parent=None):
QObject.__init__(self, parent=parent)
def do_work(self):
for _ in range(20):
print('running . . .')
QThread.sleep(2)
def main(args):
app = QApplication(args)
window = Window()
window.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main(sys.argv)

What's the correct pattern for threading in a python qt5 application?

I'm trying to write a pyqt5 application with a long running, but not CPU intensive process. I'd like to be able to run it without hanging the UI, so I'm trying to use threading, but since it doesn't seem like I can just run a thread and have it stop after its gone through its code so that it can be run again, I've tried setting up the thread to wait for a variable to change before running.
I know this can't be the correct pattern for running long processes in a pyqt app.
import time
import threading
from PyQt5 import QtWidgets, uic
class MyApp(QtWidgets.QMainWindow):
_run_thread = False
def __init__(self):
QtWidgets.QMainWindow.__init__(self)
self.ui = uic.loadUi('myapp.ui', self)
self.ui.start_thread_button.clicked.connect(self._run_thread_function)
self._thread = threading.Thread(target=self._run_thread_callback)
self._thread.daemon = True
self._thread.start()
self.ui.show()
def _run_thread_callback(self):
while True:
if self._run_thread:
print("running thread code...")
time.sleep(10)
print("thread code finished")
self._run_thread = False
def _run_thread_function(self):
print("starting thread...")
self._run_thread = True
def main():
app = QtWidgets.QApplication(sys.argv)
MyApp()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Below is a simple demo showing how to start and stop a worker thread, and safely comminucate with the gui thread.
import sys
from PyQt5 import QtCore, QtWidgets
class Worker(QtCore.QThread):
dataSent = QtCore.pyqtSignal(dict)
def __init__(self, parent=None):
super(Worker, self).__init__(parent)
self._stopped = True
self._mutex = QtCore.QMutex()
def stop(self):
self._mutex.lock()
self._stopped = True
self._mutex.unlock()
def run(self):
self._stopped = False
for count in range(10):
if self._stopped:
break
self.sleep(1)
data = {
'message':'running %d [%d]' % (
count, QtCore.QThread.currentThreadId()),
'time': QtCore.QTime.currentTime(),
'items': [1, 2, 3],
}
self.dataSent.emit(data)
class Window(QtWidgets.QWidget):
def __init__(self):
super(Window, self).__init__()
self.edit = QtWidgets.QPlainTextEdit()
self.edit.setReadOnly(True)
self.button = QtWidgets.QPushButton('Start')
self.button.clicked.connect(self.handleButton)
layout = QtWidgets.QVBoxLayout(self)
layout.addWidget(self.edit)
layout.addWidget(self.button)
self._worker = Worker()
self._worker.started.connect(self.handleThreadStarted)
self._worker.finished.connect(self.handleThreadFinished)
self._worker.dataSent.connect(self.handleDataSent)
def handleThreadStarted(self):
self.edit.clear()
self.button.setText('Stop')
self.edit.appendPlainText('started')
def handleThreadFinished(self):
self.button.setText('Start')
self.edit.appendPlainText('stopped')
def handleDataSent(self, data):
self.edit.appendPlainText('message [%d]' %
QtCore.QThread.currentThreadId())
self.edit.appendPlainText(data['message'])
self.edit.appendPlainText(data['time'].toString())
self.edit.appendPlainText(repr(data['items']))
def handleButton(self):
if self._worker.isRunning():
self._worker.stop()
else:
self._worker.start()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = Window()
window.setGeometry(500, 100, 400, 400)
window.show()
sys.exit(app.exec_())

PyQt update gui

I'm trying to update the text in a Qt GUI object via a QThread in PyQt but I just get the error QPixmap: It is not safe to use pixmaps outside the GUI thread, then it crashes. I would really appreciate any help, thanks.
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self, parent = None):
QMainWindow.__init__(self, parent)
self.setupUi(self)
self.output = Output()
def __del__ (self):
self.ui = None
#pyqtSignature("")
def on_goBtn_released(self):
threadnum = 1
#start threads
for x in xrange(threadnum):
thread = TheThread()
thread.start()
class Output(QWidget, Ui_Output):
def __init__(self, parent = None):
QWidget.__init__(self, parent)
self.setupUi(self)
self.ui = Ui_Output
self.show()
def main(self):
self.textBrowser.append("sdgsdgsgsg dsgdsg dsgds gsdf")
class TheThread(QtCore.QThread):
trigger = pyqtSignal()
def __init__(self):
QtCore.QThread.__init__(self)
def __del__(self):
self.wait()
def run(self):
self.trigger.connect(Output().main())
self.trigger.emit()
self.trigger.connect(Output().main())
This line is problematic. You are instantiating a class in the thread which looks like a widget. This is wrong. You shouldn't use GUI elements in a different thread. All GUI related code should run in the same thread with the event loop.
The above line is also wrong in terms of design. You emit a custom signal from your thread and this is a good way. But the object to process this signal should be the one that owns/creates the thread, namely your MainWindow
You also don't keep a reference to your thread instance. You create it in a method, but it is local. So it'll be garbage collected, you probably would see a warning that it is deleted before it is finished.
Here is a minimal working example:
import sys
from PyQt4 import QtGui, QtCore
import time
import random
class MyThread(QtCore.QThread):
trigger = QtCore.pyqtSignal(int)
def __init__(self, parent=None):
super(MyThread, self).__init__(parent)
def setup(self, thread_no):
self.thread_no = thread_no
def run(self):
time.sleep(random.random()*5) # random sleep to imitate working
self.trigger.emit(self.thread_no)
class Main(QtGui.QMainWindow):
def __init__(self, parent=None):
super(Main, self).__init__(parent)
self.text_area = QtGui.QTextBrowser()
self.thread_button = QtGui.QPushButton('Start threads')
self.thread_button.clicked.connect(self.start_threads)
central_widget = QtGui.QWidget()
central_layout = QtGui.QHBoxLayout()
central_layout.addWidget(self.text_area)
central_layout.addWidget(self.thread_button)
central_widget.setLayout(central_layout)
self.setCentralWidget(central_widget)
def start_threads(self):
self.threads = [] # this will keep a reference to threads
for i in range(10):
thread = MyThread(self) # create a thread
thread.trigger.connect(self.update_text) # connect to it's signal
thread.setup(i) # just setting up a parameter
thread.start() # start the thread
self.threads.append(thread) # keep a reference
def update_text(self, thread_no):
self.text_area.append('thread # %d finished' % thread_no)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
mainwindow = Main()
mainwindow.show()
sys.exit(app.exec_())

Categories

Resources