【发布时间】:2015-06-20 03:54:36
【问题描述】:
我真的很难完成编写一个需要多个不同线程的 GUI 应用程序。在高层次上,我需要:
一个 GUI 线程,它有一个按钮来打开用户选择目录的目录浏览器。在选择目录时,一个线程开始查找特定文件类型。这可能需要很长时间,所以我知道我需要放入一个单独的 QThread。
然后,一旦该 browseFile 线程完成了对文件的搜索,它就会返回一个 fileList,然后将其分块为子文件列表。然后将每个 subfileList 发送到单独的线程进行处理,这将花费大量时间。
这是我目前编写的代码:
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_())
有没有比使用 *args, **kw 更好的方法将目录路径传递给 browseThread?
如何将 fileList 返回到主线程,然后我可以将其传递给许多新的处理线程。
我确信我做的比应该做的更难,所以希望有人可以帮助我
谢谢
【问题讨论】:
标签: python multithreading pyqt pyside