call function of main thread from secondary thread

2020-04-20 17:01发布

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()

1条回答
Fickle 薄情
2楼-- · 2020-04-20 17:51

While searching for answer, qtcentre helped with it.

  1. 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.

  1. 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.

  1. 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.

查看更多
登录 后发表回答