从辅助线程调用主线程函数 [英] call function of main thread from secondary thread

查看:73
本文介绍了从辅助线程调用主线程函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 PyQt 中制作一个 GUI,供用户创建大量数据的备份.

GUI(主线程)正在接受用户的输入.rsync 命令(用于备份)也在主线程中被调用,因此窗口被冻结.

目标是尝试使用 qthread,使应用程序运行时不会冻结.

我的搜索材料:

1:https://www.youtube.com/watch?v=o81Q3oyz6rg.该视频展示了如何通过在辅助线程中运行其他任务来不冻结 GUI.我试过了,它有效.但这对在工作线程中运行命令没有帮助.

尽管在辅助线程中调用了 rsync,gui 仍然冻结.我做错了什么?.

导入系统从 PyQt4 导入 QtCore、QtGui从 backUpUi 导入 Ui_MainWindow导入线程,队列类调用者线程(QtCore.QThread):def __init__(self, func, parent=None, *args, **kwargs):super(callerThread, self).__init__(parent)self._func = funcself._args = argsself._kwargs = kwargs定义运行(自我):self._func(*self._args, **self._kwargs)类监视器(QtCore.QObject):updateText = QtCore.pyqtSignal(str)def update_list(self):t_monitor = callerThread(self.monitor_vector, parent=self)t_monitor.daemon = 真t_monitor.start()def monitor_vector(self):self.updateText.emit('更新列表')类 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, "线程开始!", 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("工作线程已完成处理 %s" % text1)def exitWindow(self):self.ui.close()定义主():app = QtGui.QApplication(sys.argv)对话框 = backUpMain()对话框显示()sys.exit(app.exec_())如果 __name__ == '__main__':主要的()

解决方案

在寻找答案时,qtcentre 提供了帮助.

  1. 你需要有单独的信号类

<块引用>

class MySignal(QtCore.QObject):sig = QtCore.pyqtSignal(列表)sigStr = QtCore.pyqtSignal(str)

该信号用于线程间通信.

  1. 从主线程到工作线程的通信,

    在定义了 Ui 的类的 init 中创建 qthread 的实例在 init 或需要时从主线程传递参数.

<块引用>

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

通过这种方式,数据从 main 传递到 worker.

  1. 从工作线程到主线程通信

<块引用>

类 MyThread(QtCore.QThread):

 def __init__(self, parent = None, *args, **kw): ....self.signal = MySignal()

需要执行不同类型的信号(列表、字符串 ...)在 MySignal() 中定义

def xyz(self): self.signal.sigStr.emit(message)

希望这会有所帮助.

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.

  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.

这篇关于从辅助线程调用主线程函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆