Python 从pyqt线程内部启动多个线程
我目前有一个脚本,它将一个文件夹中的文件(数千个文件)读入一个列表,然后将该列表拆分为4个子列表。然后为每个列表运行一个线程。这在python脚本中很容易实现Python 从pyqt线程内部启动多个线程,python,multithreading,pyqt,pyside,Python,Multithreading,Pyqt,Pyside,我目前有一个脚本,它将一个文件夹中的文件(数千个文件)读入一个列表,然后将该列表拆分为4个子列表。然后为每个列表运行一个线程。这在python脚本中很容易实现 thread1fileList, thread2fileList, thread3fileList, thread4fileList = load.sortFiles(fileList) threads = [threading.Thread(target=load.threadLoad, args=(fileList, )) for f
thread1fileList, thread2fileList, thread3fileList, thread4fileList = load.sortFiles(fileList)
threads = [threading.Thread(target=load.threadLoad, args=(fileList, ))
for fileList in (thread1fileList, thread2fileList, thread3fileList, thread4fileList)]
for thread in threads:
thread.start()
for thread in threads:
thread.join()
不过,我现在使用Pyside将此代码移动到GUI中
我已经能够创建一个线程来读取文件夹中的文件(以确保GUI仍然响应),但是我无法从Qt.thread内部启动4个新线程来处理文件列表
这是我的核心代码
self.unzipThread = UnzipThread()
self.unzipThread.start()
self.unzipThread.finished.connect(self.finishUp()) #finsihUp provides the final page of the wizard
class UnzipThread(QtCore.QThread):
def __init__(self):
QtCore.QThread.__init__(self)
def run(self):
for dirname, dirnames, filenames in os.walk(directorypath):
for filename in filenames:
if filename.endswith(".zip"):
fileList.append(filename)
count = count +1
如果我尝试在run函数中添加启动线程,它会导致一个错误,指出这是不允许的
我还能怎样做到这一点
谢谢
编辑##
完整示例
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
from PySide import QtGui, QtCore
import os
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
runButton = QtGui.QPushButton("Run")
runButton.clicked.connect(self.unzip)
exitButton = QtGui.QPushButton("Exit")
exitButton.clicked.connect(QtCore.QCoreApplication.instance().quit)
hbox = QtGui.QHBoxLayout()
hbox.addWidget(runButton)
hbox.addStretch(1)
hbox.addWidget(exitButton)
titleBox = QtGui.QVBoxLayout()
titleLabel = QtGui.QLabel(self.tr("Find Zip File"))
searchBox = QtGui.QHBoxLayout()
self.fileLabel = QtGui.QLabel(self.tr("Location: "))
self.fileLineEdit = QtGui.QLineEdit()
self.fileDialogBtn = QtGui.QPushButton("Browse")
self.fileDialogBtn.clicked.connect(self.openDirectoryDialog)
searchBox.addWidget(self.fileLabel)
searchBox.addWidget(self.fileLineEdit)
searchBox.addWidget(self.fileDialogBtn)
titleBox.addWidget(titleLabel)
titleBox.addStretch(1)
titleBox.addLayout(searchBox)
titleBox.addStretch(1)
titleBox.addLayout(hbox)
self.setLayout(titleBox)
self.resize(500, 300)
self.center()
self.setWindowTitle('Example')
self.show()
def unzip(self):
self.unzipThread = UnzipThread(self.filePath)
self.unzipThread.start()
self.unzipThread.finished.connect(self.finishUp)
def openDirectoryDialog(self):
flags = QtGui.QFileDialog.DontResolveSymlinks | QtGui.QFileDialog.ShowDirsOnly
directory = QtGui.QFileDialog.getExistingDirectory(self, "Open Directory", os.getcwd(),flags)
self.fileLineEdit.setText(directory)
self.filePath = self.fileLineEdit.text()
def center(self):
qr = self.frameGeometry()
cp = QtGui.QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp)
self.move(qr.topLeft())
def closeEvent(self, event):
reply = QtGui.QMessageBox.question(self, 'Message',
"Are you sure to quit?", QtGui.QMessageBox.Yes |
QtGui.QMessageBox.No, QtGui.QMessageBox.No)
if reply == QtGui.QMessageBox.Yes:
event.accept()
else:
event.ignore()
def finishUp(self):#yet to code up to GUI log windows
print "finished"
class UnzipThread(QtCore.QThread):
def __init__(self, filepath):
QtCore.QThread.__init__(self)
self.filePath = filepath
def run(self):
fileList = []
count = 0
for dirname, dirnames, filenames in os.walk(self.filePath):
for filename in filenames:
if filename.endswith(".shp"):
fileList.append(filename)
count += 1
print count
list1 = []
list2 = []
for shpfile in fileList:
if "line" in shpfile:
list1.append(shpfile)
elif "point" in shpfile:
list2.append(shpfile)
else:
pass
self.processThread1 = ProcessThread1(list1)
self.processThread1.start()
self.processThread1.finished.connect(self.threadCount)
self.processThread2 = ProcessThread2(list2)
self.processThread2.start()
self.processThread2.finished.connect(self.threadCount)
return
def threadCount(self):
print "got here"
class ProcessThread1(QtCore.QThread):
def __init__(self, filelist):
QtCore.QThread.__init__(self)
self.filelist = filelist
def run(self):
count = 0
for textfile in self.filelist:
count +=1
print "thread 1 count %s" %(count)
return
class ProcessThread2(QtCore.QThread):
def __init__(self, filelist):
QtCore.QThread.__init__(self)
self.filelist = filelist
def run(self):
count = 0
for textfile in self.filelist:
count +=1
print "thread 2 count %s" %(count)
return
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
在剥离大量代码以提供此示例的过程中,它似乎是从QT内部运行线程。线程可以工作,但初始线程认为它在运行的线程完成之前已经完成
所以我得到的结果是这样的
5635
已完成线程1计数2858
线程2在这里计数2777
这里
因此,您可以看到注释“finished”出现在另一个代码“threadcount1”之前,这证明它认为它已经完成了
不管怎样,我能让他们交流吗
谢谢
编辑2
因此,我现在已经将代码更改为使用QObject和movetoThread,但是如何正确地确定这两个子线程都已完成。这是我的代码。我不得不使用线程计数器和While循环来确定两个子线程是否都已完成。一定有更好的办法
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
from PySide import QtGui, QtCore
import os
from PySide.QtCore import Signal as pyqtSignal
import time
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
runButton = QtGui.QPushButton("Run")
runButton.clicked.connect(self.unzip)
exitButton = QtGui.QPushButton("Exit")
exitButton.clicked.connect(QtCore.QCoreApplication.instance().quit)
hbox = QtGui.QHBoxLayout()
hbox.addWidget(runButton)
hbox.addStretch(1)
hbox.addWidget(exitButton)
titleBox = QtGui.QVBoxLayout()
titleLabel = QtGui.QLabel(self.tr("Find Zip File"))
searchBox = QtGui.QHBoxLayout()
self.fileLabel = QtGui.QLabel(self.tr("Location: "))
self.fileLineEdit = QtGui.QLineEdit()
self.fileDialogBtn = QtGui.QPushButton("Browse")
self.fileDialogBtn.clicked.connect(self.openDirectoryDialog)
searchBox.addWidget(self.fileLabel)
searchBox.addWidget(self.fileLineEdit)
searchBox.addWidget(self.fileDialogBtn)
titleBox.addWidget(titleLabel)
titleBox.addStretch(1)
titleBox.addLayout(searchBox)
titleBox.addStretch(1)
titleBox.addLayout(hbox)
self.setLayout(titleBox)
self.resize(500, 300)
self.center()
self.setWindowTitle('Example')
self.show()
def unzip(self):
thread = self.thread = QtCore.QThread()
worker = self.worker = Worker(self.filePath)
worker.moveToThread(thread)
worker.finished.connect(worker.deleteLater)
worker.finished.connect(thread.quit)
thread.started.connect(worker.run)
thread.finished.connect(self.finishUp)
thread.start()
def openDirectoryDialog(self):
flags = QtGui.QFileDialog.DontResolveSymlinks | QtGui.QFileDialog.ShowDirsOnly
directory = QtGui.QFileDialog.getExistingDirectory(self, "Open Directory", os.getcwd(),flags)
self.fileLineEdit.setText(directory)
self.filePath = self.fileLineEdit.text()
def center(self):
qr = self.frameGeometry()
cp = QtGui.QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp)
self.move(qr.topLeft())
def closeEvent(self, event):
reply = QtGui.QMessageBox.question(self, 'Message',
"Are you sure to quit?", QtGui.QMessageBox.Yes |
QtGui.QMessageBox.No, QtGui.QMessageBox.No)
if reply == QtGui.QMessageBox.Yes:
event.accept()
else:
event.ignore()
def finishUp(self):#yet to code up to GUI log windows
print "unzip thread finished"
class Worker(QtCore.QObject):
finished = pyqtSignal()
def __init__(self, filepath):
QtCore.QObject.__init__(self)
self.filePath = filepath
def run(self):
self.threadcount = 0
print "finding files"
fileList = []
count = 0
for dirname, dirnames, filenames in os.walk(self.filePath):
for filename in filenames:
if filename.endswith(".shp"):
fileList.append(filename)
count += 1
print count
self.list1 = []
self.list2 = []
for shpfile in fileList:
if "line" in shpfile:
self.list1.append(shpfile)
elif "point" in shpfile:
self.list2.append(shpfile)
else:
pass
thread1 = self.thread1 = QtCore.QThread()
worker1 = self.worker1 = Process1(self.list1)
worker1.moveToThread(thread1)
worker1.finished.connect(worker1.deleteLater)
worker1.finished.connect(thread1.quit)
thread1.started.connect(worker1.run)
thread1.finished.connect(self.finishUp)
thread1.start()
thread2 = self.thread2 = QtCore.QThread()
worker2 = self.worker2 = Process2(self.list2)
worker2.moveToThread(thread2)
worker2.finished.connect(worker2.deleteLater)
worker2.finished.connect(thread2.quit)
thread2.started.connect(worker2.run)
thread2.finished.connect(self.finishUp)
thread2.start()
def finishUp(self):
self.threadcount += 1
while True:
if self.threadcount != 2:
break
else:
print "extra threads finished"
self.finished.emit()
break
class Process1(QtCore.QObject):
finished = pyqtSignal()
def __init__(self, filelist):
QtCore.QObject.__init__(self)
self.filelist = filelist
def run(self):
count = 0
for textfile in self.filelist:
count +=1
print "thread 1 count %s" %(count)
self.finished.emit()
class Process2(QtCore.QObject):
finished = pyqtSignal()
def __init__(self, filelist):
QtCore.QObject.__init__(self)
self.filelist = filelist
def run(self):
count = 0
for textfile in self.filelist:
count +=1
time.sleep(15)
print "thread 2 count %s" %(count)
self.finished.emit()
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
首先,注释“finished”出现在“thread count x”之前,因为UnzipThread确实在ProcessingThreads之前完成。这是意料之中的
第二,我不能说是否有沟通问题,但一定要阅读有关的信息。由于信号问题,不建议子类化QThread。最好使用辅助对象并通过QObject:movetoThread()
将它们移动到线程
如果仍有疑问,请在所有可能的位置打印
QThread.currentThread()
,并检查哪个线程中实际运行的代码。是否尝试从UnzipThread中启动python线程或另一个QThread?是,确保它与GUI解耦。我的另一个想法是运行UnzipThread并返回列表,然后将列表传递给4个新线程,但我无法通过信号返回文件列表。也许你应该尝试发布一个完整的示例,这会产生错误。我从一个QThread中生成新线程没有问题…@sebastian我添加了一个完整的示例,它应该突出显示问题感谢您的帮助。我现在已经实现了moveToThread(),但很难知道在完成UnzipThread之前两个子线程都已经完成了。有什么想法吗?感谢线程和工作线程只是局部变量,过早地收集了垃圾。通过使用self使它们成为类变量。
ok可以,这有助于了解两个子线程何时完成?我的finishUp函数和threadcount不能作为确定线程已完成的常规方法。有更好的办法吗?感谢您的帮助,Signals
,slot
和QMutex
锁是线程交互的标准方式。再也没有魔法了。以你为例,我不理解这个问题。你应该清楚地写下你所期望的,为什么以及你所看到的。您想查看子线程何时完成?只要发出一个信号,当他们这样做。