简体   繁体   English

如何将变量传递给 QThread(PySide 或 PyQT)

[英]How to pass variable to QThread (PySide or PyQT)

I am really struggling to finish writing a GUI app which requires several different threads.我真的很难完成需要几个不同线程的 GUI 应用程序的编写。 At a high level I require:在高层次上,我要求:

A GUI Thread, which has a button to open a directory browser which the user selects a directory.一个 GUI 线程,它有一个按钮来打开用户选择目录的目录浏览器。 On selecting the directory a thread starts which looks for a particular file type.在选择目录时,一个线程开始寻找特定的文件类型。 This may take a long time so I know I need to put in a separate QThread.这可能需要很长时间,所以我知道我需要放入一个单独的 QThread。

Then once that browseFile thread has finished searching for files it returns a fileList, which is then chunked into sub fileLists.然后,一旦该 browseFile 线程完成对文件的搜索,它就会返回一个文件列表,然后将其分块到子文件列表中。 Each subfileList is then sent to a separate thread for processing which will take lots of time.然后将每个子文件列表发送到单独的线程进行处理,这将花费大量时间。

This is what I have coded so far:这是我到目前为止编码的内容:

from PyQt4 import QtCore
from PyQt4 import QtGui
from PyQt4.QtCore import * 
import os

class BrowseThread(QThread):
    processdone = QtCore.pyqtSignal("QString") # Define custom signal.

    def __init__(self, parent, *args, **kw):
        QThread.__init__(self, parent)
        self.search(*args, **kw)

    def search(self, directory_path):
        self.fileList = []
        self.count = 0
        for dirname, dirnames, filenames in os.walk(directory_path):
            for filename in filenames:
                if filename.endswith(".gz"):
                    self.fileList.append(os.path.join(directory_path,filename))

        self.emit( SIGNAL('processdone'), "DONE")

        return     

class MyClass(QObject):

    def __init__(self):            
        super(MyClass, self).__init__()

        directory_path = r'C:\Data'

        thread1 = BrowseThread(self, directory_path) 
        self.connect( thread1, SIGNAL("processdone"), self.thread1done)  
        thread1.start()  

    def thread1done(self, text):
        print(text)
        sys.exit()

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)  
    a = MyClass()
    sys.exit(app.exec_())

Is there a better way to pass the directory path to the browseThread than using *args, **kw?有没有比使用 *args, **kw 更好的方法将目录路径传递给 browseThread?

How can I return the fileList back to the main thread which I can then pass to a number of new processing threads.如何将 fileList 返回到主线程,然后我可以将其传递给许多新的处理线程。

I am sure I making this harder than it should be so hopefully someone can help me我确信我做得比它应该更难,所以希望有人能帮助我

thanks谢谢

I suggest to change the design of your code to a solution with a worker class which does the real work.我建议将您的代码设计更改为具有真正工作的工人类的解决方案。 This worker class will be moved to a normal thread like so (I don't know PyQt but it should be fairly similar to the C++ version):这个工作类将被移动到一个普通线程中(我不知道 PyQt 但它应该与 C++ 版本非常相似):

class SearchWorker(QObject):
    finished_sig = QtCore.pyqtSignal('')  # finished signal

    def __init__(self, your_stuff):
        self.directory_path = ...

    def process_slot(self):
        # use self.directory_path
        # your code
        self.emit(SIGNAL('finished_sig'), '')

Now the main part is to connect the start signal to your worker in your class MyClass.现在主要部分是将开始信号连接到您的类 MyClass 中的工作人员。 Which gives something like this in C++:这在 C++ 中给出了这样的东西:

QThread* thread = new QThread;
Worker* worker = new Worker();
worker->moveToThread(thread);
// The start signal will actually launch your search method in the worker
connect(thread, SIGNAL(started()), worker, SLOT(process_slot()));
connect(worker, SIGNAL(finished()), thread, SLOT(quit()));

// May be useless in python
connect(worker, SIGNAL(finished_sig()), worker, SLOT(deleteLater()));
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
thread->start();

The good point is that you can use the worker with or without a thread and you can easily pass parameters to the worker via setter methods.好处是您可以在有或没有线程的情况下使用工作线程,并且可以通过 setter 方法轻松地将参数传递给工作线程。

To know more about Qt signals and slots for threads I suggest you to read:要了解有关线程的 Qt 信号和插槽的更多信息,我建议您阅读:

You should be able to use QT's signal and slot mechanism.您应该能够使用 QT 的信号和槽机制。

http://doc.qt.io/qt-5/threads-qobject.html#signals-and-slots-across-threads http://doc.qt.io/qt-5/threads-qobject.html#signals-and-slots-across-threads

You can also use a message passing system.您还可以使用消息传递系统。

There is nothing wrong with passing it as an arg either.将它作为 arg 传递也没有错。 Just make sure you do it safely: https://web.archive.org/web/20150423115746/http://effbot.org/zone/thread-synchronization.htm只要确保你安全地做: https : //web.archive.org/web/20150423115746/http : //effbot.org/zone/thread-synchronization.htm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM