call function of main thread from secondary thread - pyqt4

I am making a GUI in PyQt for user to create backup of huge data.
The GUI ( main thread ) is taking inputs from user. rsync command ( for backup ) is also being called in main thread hence the window is freezing.
Aim is to try qthread such that app runs without freezing.
My search material :
1 : https://www.youtube.com/watch?v=o81Q3oyz6rg. This video shows how to not freeze GUI by running other task in secondary thread. I've tried it and it works. But it does not help in running the command in worker thread.
Inspite of calling rsync in secondary thread, the gui still freezes. What am I doing wrong ?.
import sys
from PyQt4 import QtCore, QtGui
from backUpUi import Ui_MainWindow
import threading, Queue
class callerThread(QtCore.QThread):
def __init__(self, func, parent=None, *args, **kwargs):
super(callerThread, self).__init__(parent)
self._func = func
self._args = args
self._kwargs = kwargs
def run(self):
self._func(*self._args, **self._kwargs)
class Monitor(QtCore.QObject):
updateText = QtCore.pyqtSignal(str)
def update_list(self):
t_monitor = callerThread(self.monitor_vector, parent=self)
t_monitor.daemon = True
t_monitor.start()
def monitor_vector(self):
self.updateText.emit('updated list')
class backUpMain(QtGui.QMainWindow):
def __init__(self,parent=None):
super(backUpMain, self).__init__(parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.connect(self.ui.okButton, QtCore.SIGNAL("clicked()"), self.startThread)
self.ui.cancelButton.released.connect(sys.exit)
self.monitor = Monitor()
def _handlebackUpdate(self, txt):
QtGui.QMessageBox.information(self, "thread started!", txt)
self.ui.logEdit.clear()
self.ui.logEdit.setText(txt)
def startThread(self):
self.monitor = Monitor()
self.monitor.updateText.connect(self._handlebackUpdate)
self.monitor.update_list()
def threadDone(self,text1):
self.ui.logEdit.append("Worker Thread finished processing %s" % text1)
def exitWindow(self):
self.ui.close()
def main():
app = QtGui.QApplication(sys.argv)
dialog = backUpMain()
dialog.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()

While searching for answer, qtcentre helped with it.
You need to have seperate class for signals
class MySignal(QtCore.QObject):
sig = QtCore.pyqtSignal(list)
sigStr = QtCore.pyqtSignal(str)
This signal is used to communicate between threads.
to communicate from main thread to worker thread,
create instance of qthread in init of class where Ui is defined
to pass parameters from main thread either in init or where required.
class MyThread(QtCore.QThread):
def __init__(self, parent = None, *args, **kw): . .
self.setData(*args, **kw)
def setData(self, userShotList, inData, outData, dept):
self.userShotList = userShotList . .
This way data is passed from main to worker.
to communicate from worker thread to main thread
class MyThread(QtCore.QThread):
def __init__(self, parent = None, *args, **kw): . . . .
self.signal = MySignal()
where required execute signal with different types ( list, str ...)
defined in MySignal()
def xyz(self): self.signal.sigStr.emit(message)
Hope this helps.

Related

PyQt, QThread: GUI freeze with large amount of SIGNAL from another thread (QListWidget massive update)

I have an app that starts the GUI and performs "the heavy part" of the code in another thread using QThread. In this thread I emit a SIGNAL that is connected to the GUI Class and that performs addItem on QListWidget.
There are a massive "signaling" from this thread to the GUI and it "freeze".
Is there a way to avoid this? Have I to use another mini GUI in different thread only for QListWidget?
Thanks
EDIT:
This is the thread that execute the heavy logic
class YourThreadName(QThread):
def __init__(self, some variables):
QThread.__init__(self)
def __del__(self):
self.wait()
def run(self):
# Here there is a for cycle that emits a SIGNAL
for ... :
...
self.emit(SIGNAL("needed_variable"), needed_variable)
...
In the GUI Class there are some methods, particularly:
class GUI(QtGui.QMainWindow, GUI.Ui_MainWindow):
def __init__(self, parent=None):
super(GUI, self).__init__(parent)
self.setupUi(self)
def ... (self):
...
def start_main_code(self):
self.new_thread = YourThreadName(some variables)
self.connect(self.new_thread, SIGNAL("finished()"), self.done)
self.connect(self.new_thread, SIGNAL("needed_variable"), self.show_variable)
self.new_thread.start()
def show_variable(self, data):
self.QListWidget_object.addItem(data)
def ... (self):
...
The script below is a Minimal, Complete, and Verifiable Example based on the information currently given in your question and comments. It emits data from a worker thread every 10ms and updates a list-widget in the GUI. On my Linux system (using Python-3.6.3, Qt-4.8.7 and PyQt-4.12.1) it does not block or freeze the GUI. There is obviously some flickering whilst the list-widget is being updated, but I am able to select items, scroll up and down, click the button, etc. And if I increase the sleep to 25ms, I don't even get any flickering.
UPDATE:
The performance can be improved by using setUniformItemSizes and sending the messages in batches. On my system, after a slight initial delay, the list populates with fifty thousand items almost instantly.
import sys
from PyQt4 import QtCore, QtGui
class Worker(QtCore.QThread):
message = QtCore.pyqtSignal(object)
def run(self):
batch = []
for index in range(50000):
if len(batch) < 200:
batch.append(index)
continue
self.message.emit(batch)
batch = []
class Window(QtGui.QWidget):
def __init__(self):
super(Window, self).__init__()
self.listWidget = QtGui.QListWidget()
self.listWidget.setUniformItemSizes(True)
self.button = QtGui.QPushButton('Start')
self.button.clicked.connect(self.handleButton)
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.listWidget)
layout.addWidget(self.button)
self.worker = Worker()
self.worker.message.connect(self.handleMessages)
def handleMessages(self, batch):
for message in batch:
self.listWidget.addItem('Item (%s)' % message)
def handleButton(self):
if not self.worker.isRunning():
self.worker.start()
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
window = Window()
window.setGeometry(600, 50, 200, 400)
window.show()
sys.exit(app.exec_())

Threading a method for a PyQT GUI

I have a pyqt GUI and a method[BigramClassification()]which causes the GUI to hang for several seconds. Therefore, i figured out threading need to be used. So after reading several tutorials i came up with the following code.
import sys,os
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import QThread
import time
class MyForm(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.lblHistory.setPixmap(QtGui.QPixmap(os.getcwd() + "/historygraph.png"))
self.workerThread=WorkingThread()
self.ui.pushButton.clicked.connect(self.generateDetails)
self.ui.btnsubmitsettings.clicked.connect(self.addDetails)
def generateDetails(self):
self.workerThread.start()
self.ui.lblHistory.setPixmap(QtGui.QPixmap(os.getcwd() + "/historygraph.png"))
self.addPiechart()
self.addWordCloud()
self.summaryText()
def addPiechart(self):
print ("Added")
def addWordCloud(self):
print ("Added")
def addDetails(self):
def summaryText(self):
print("Added")
class WorkingThread(QThread):
def __init__(self, parent=None):
super(self.__class__, self).__init__(parent)
def run(self):
BigramsClassifier()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = MyForm()
myapp.show()
sys.exit(app.exec_())
The problem i have is when i run this and click on pushButton the thread starts but also executes the methods after the start() as seen in def generateDetails(self): I need to prepare this code so that the methods in def generateDetails(self): executes after the thread is done executing with the heavy method BigramClassification()execution.
Summary How can i stop the auto execution of the methods in def generateDetails(self): but only after the method BigramClassification() is completed.
EDIT This error is thrown up when i try to close the GUI.
Connect a slot to the thread's finished signal which can performs other actions once the long-running task is completed:
class MyForm(QtGui.QMainWindow):
def __init__(self, parent=None):
...
self.workerThread = WorkingThread()
self.workerThread.finished.connect(self.doOtherStuff)
...
def generateDetails(self):
if not self.workerThread.isRunning():
self.workerThread.start()
def doOtherStuff(self):
self.ui.lblHistory.setPixmap(QtGui.QPixmap(os.getcwd() + "/historygraph.png"))
self.addPiechart()
self.addWordCloud()
self.summaryText()

How can I send a list object from a QThread thread to the UI's main thread?

I have written this example code to try and figure out how to communicate between a background thread, and the main thread. As I understand it, a thread cannot simply interact with the UI or variables existing in a different thread.
I would like to grab the list 'data' in the background thread, then display it on 'lbl2' in the UI. If this program is run as is, it will print the result I want on the command line.
Data received in background thread = [1, 2, 3, 4, 5]
Q1: What is the correct way to send data such as a list or string, from the background thread to the main thread?
Q2: How would I begin to implement that in the example code?
#!/usr/bin/env python3.4
from PySide.QtGui import QPushButton, QApplication, QWidget, QLabel
from PySide.QtCore import QThread, QCoreApplication
import queue
import sys
class Gui(QWidget):
def __init__(self):
super(Gui, self).__init__()
self.initUI()
def initUI(self):
lbl1 = QLabel('Data Recieved =', self)
lbl2 = QLabel('None', self)
lbl2.move(85, 0)
lbl2.resize(100, 15)
qbtn = QPushButton('Quit', self)
qbtn.clicked.connect(QCoreApplication.instance().quit)
qbtn.move(0, 20)
btn = QPushButton('Get Summary', self)
btn.move(100, 20)
btn.clicked.connect(lambda: bgThread.summary())
self.setGeometry(300, 300, 200, 50)
self.setWindowTitle('Thread Communication Example')
self.show()
class BackgroundThread(QThread):
def __init__(self, q, loop_time=1.0/60):
self.q = q
self.timeout = loop_time
super(BackgroundThread, self).__init__()
def onThread(self, function, *args, **kwargs):
self.q.put((function, args, kwargs))
def run(self):
while True:
try:
function, args, kwargs = self.q.get(timeout=self.timeout)
function(*args, **kwargs)
except queue.Empty:
self.idle()
def idle(self):
pass
def _summary(self):
# Run function which will return a list object
# data = externalclass.summary()
# Need to send list:'data' to the main thread.
data = [1, 2, 3, 4, 5]
print('Data received in background thread =', data)
def summary(self):
self.onThread(self._summary)
if __name__ == "__main__":
app = QApplication(sys.argv)
# Setup background thread
request_queue = queue.Queue()
bgThread = BackgroundThread(request_queue)
bgThread.start()
# Setup Gui
ui = Gui()
sys.exit(app.exec_())
You can define a custom signal, which can be safely emitted across threads:
from PySide.QtCore import Signal
class Gui(QWidget):
def initUI(self):
...
bgThread.dataReceived.connect(lambda data: lbl2.setText(str(data)))
class BackgroundThread(QThread):
dataReceived = Signal(list)
...
def _summary(self):
...
self.dataReceived.emit(data)

How to write a multi-threaded Pyside application

What is the simplest way to multi-thread a Pyside application, so the GUI can be operational and the thread will still run?
Thread class:
class MyLongThread(QThread):
def __init__(self, parent = None):
QThread.__init__(self, parent)
self.exiting = False
def run(self):
while 1:
self.msleep(100)
print("run")
Complete .pyw
import sys,time
from PySide.QtGui import *
from PySide.QtCore import *
from PySide.QtWebKit import *
def thread():
global threade
threade = MyLongThread()
threade.run()
def thread_terminate():
global threade
threade.terminate()
class MyLongThread(QThread):
def __init__(self, parent = None):
QThread.__init__(self, parent)
self.exiting = False
def run(self):
while 1:
self.msleep(100)
print("run")
app = QApplication(sys.argv)
wid = QWidget()
wid.resize(250, 400)
wid.setWindowTitle('Threaded Program')
#wid.setWindowIcon(QIcon('web.png'))
#### BUTTONS
btn = QPushButton('Stop', wid)
btn.setToolTip('Stop the thread.') ## Stop the thread
btn.resize(btn.sizeHint())
btn.move(147, 50)
btn.clicked.connect(thread_terminate)
qbtn = QPushButton('Start', wid)
qbtn.setToolTip('Start the thread.') ## End the Thread
qbtn.resize(btn.sizeHint())
qbtn.move(27, 50)
qbtn.clicked.connect(thread)
####
#### LABEL
label = QLabel('Start The Thread :)',wid)
label.resize(label.sizeHint())
label.move(28, 15)
####
wid.show()
sys.exit(app.exec_())
When I run the code and press the start button, it freezes the gui but prints run.
Don't directly call thread.run() as this executes the method in the main thread. Instead, call thread.start() which will launch a thread and start executing your run() method in the thread.

PySide: Threading causes GUI to crash

I'm trying to learn the basics of threading with PySide, and so put together the below code. What I'm trying to do is launch a thread that will update a QPlainTextEdit widget using a list of string, with a delay between each string. Instead what I'm getting is a crash to desktop, and I can't understand why:
import sys
import time
from PySide import QtCore, QtGui
class Worker(QtCore.QThread):
to_log = QtCore.Signal(str)
def __init__(self, txt, parent=None):
super(Worker, self).__init__(parent)
self.txt = txt
def run(self):
for i in self.txt:
self.to_log.emit(i)
time.sleep(1)
class TestThreadsApp(QtGui.QWidget):
def __init__(self):
super(TestThreadsApp, self).__init__()
self.initUI()
def initUI(self):
self.log = QtGui.QPlainTextEdit()
self.pb = QtGui.QPushButton('Go')
hbox = QtGui.QHBoxLayout()
hbox.addWidget(self.log)
hbox.addWidget(self.pb)
self.setLayout(hbox)
self.setGeometry(300, 300, 300, 150)
self.setWindowTitle('Test')
self.show()
self.pb.clicked.connect(self.get_worker)
def get_worker(self):
self.proceed = False
worker = Worker(['This is a test', 'to understand threading'])
worker.to_log.connect(self.to_log)
worker.start()
def to_log(self, txt):
self.log.appendPlainText(txt)
def main():
app = QtGui.QApplication(sys.argv)
ex = TestThreadsApp()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
If I update the get_worker() method to the below, it will run, but the QPlainTextEdit widget is updated with all the strings simultaneously, where as the behavior I'm wanting is for the widget to be updated by the threaded processes as each string is emitted - not altogether after both have been emitted:
def get_worker(self):
self.proceed = False
worker = Worker(['This is a test', 'to understand threading'])
worker.to_log.connect(self.to_log)
worker.start()
while not worker.isFinished():
pass
You need to keep a reference to the thread, otherwise it will be garbage-collected as soon as get_worker returns.
So do something like this, instead:
def get_worker(self):
self.worker = Worker(['This is a test', 'to understand threading'])
self.worker.to_log.connect(self.to_log)
self.worker.start()

Resources