首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为什么QtNetwork.QFtp.get下载在多个文件的for循环中失败?

为什么QtNetwork.QFtp.get下载在多个文件的for循环中失败?
EN

Stack Overflow用户
提问于 2016-12-27 18:48:13
回答 2查看 139关注 0票数 0

我正在尝试使用for循环从ftp站点下载多个文件。在弹出python.exe关闭窗口之前,以下代码似乎只适用于循环中的前2个文件。两个下载的文件是完美的,但第三个下载的文件在关机时是空的。剩下的文件我都搞不懂。你知道问题出在哪里吗?

代码语言:javascript
运行
复制
from PyQt4 import QtCore, QtGui, QtNetwork


class FtpWindow(QtGui.QDialog):

    def __init__(self, parent=None):
        self.fileList = QtGui.QTreeWidget()
        self.ftp = QtNetwork.QFtp(self)
        self.progressDialog = QtGui.QProgressDialog(self)
        self.downloadAllButton.clicked.connect(self.downloadAllFile)
        self.ftp.commandFinished.connect(self.ftpCommandFinished)

    def downloadAllFile(self):
        for jj in range(self.fileList.topLevelItemCount()): # how many files in a particular folder
            fileName = self.fileList.topLevelItem(jj).text(0) 
            self.outFile = QtCore.QFile(fileName)

            self.ftp.get(fileName, self.outFile) #download one file at a time
            self.progressDialog.setLabelText("Downloading %s..." % fileName)    
            self.progressDialog.exec_()

    def ftpCommandFinished(self, _, error):
        self.setCursor(QtCore.Qt.ArrowCursor)
        if self.ftp.currentCommand() == QtNetwork.QFtp.Get:
            if error:
                self.statusLabel.setText("Canceled download of %s." % self.outFile.fileName())
                self.outFile.close()
                self.outFile.remove()
            else:
                self.statusLabel.setText("Downloaded %s to current directory." % self.outFile.fileName())
                self.outFile.close()

            self.outFile = None
            self.enableDownloadButton()
            self.progressDialog.hide()
EN

回答 2

Stack Overflow用户

发布于 2017-01-03 11:05:39

self.progressDialog.exec_()应该是阻塞模式对话框。对非阻塞调用使用self.progressDialog.show()

看起来ftp get是非阻塞的,所以您必须等待,直到使用commandFinished()信号完成下载。

我的猜测是,循环中的每个迭代都会覆盖self.outFile,因此没有任何python对对象的引用。这使得对象在python执行垃圾回收时就会死掉。我的猜测是,前两个文件较小且快速,第三个文件较大,因此其他文件可以在垃圾回收之前下载。对于最后一个文件,要么是垃圾收集,要么是更快。

http://pyside.github.io/docs/pyside/PySide/QtNetwork/QFtp.html#PySide.QtNetwork.PySide.QtNetwork.QFtp.get

代码语言:javascript
运行
复制
class FtpWindow(QtGui.QDialog):

    def __init__(self, parent=None):
        self.fileList = QtGui.QTreeWidget()
        self.ftp = QtNetwork.QFtp(self)
        self.progressDialog = QtGui.QProgressDialog(self)
        self.progressDialog.canceled.connect(self.ftp.abort)
        self.downloadAllButton.clicked.connect(self.downloadAllFile)
        self.ref_holder = {}
        self.ftp.commandFinished.connect(self.ftpCommandFinished)

    def download_file(self, filename):
        """Non blocking start downloading a file."""
        outFile = QtCore.QFile(filename)
        cmd_id = self.ftp.get(filename, outFile) # Non blocking just start downloading

        # This keeps the object alive and doesn't overwrite them.
        self.ref_holder[cmd_id] = [filename, outFile]

    def downloadAllFile(self):
        self.progressDialog.reset()
        num_downloads = self.fileList.topLevelItemCount()
        self.progressDialog.setMaximum(num_downloads)
        self.progressDialog.setValue(0)
        self.progressDialog.setLabelText("Downloading %d files ..." % num_downloads)
        self.progressDialog.show()
        for jj in range(num_downloads): # how many files in a particular folder
            fileName = self.fileList.topLevelItem(jj).text(0) 
            self.download_file(fileName) # Non blocking, and doesn't overwrite self.outFile with every iteration

    def ftpCommandFinished(self, cmd_id, error=None):
        """Increased the number of items finished."""
        self.progressDialog.setValue(self.progressDialog.value()+1)
        item = self.ref_holder.pop(cmd_id) # Remove the reference for the finished item
        if error:
            self.progressDialog.setLabelText("Error downloading %s" % item[0])

        # Check if all downloads are done
        if len(self.ref_holder) == 0:
            self.progressDialog.setValue(self.progressDialog.maximium())
            self.progressDialog.close() # This shouldn't be needed

我上面的示例将保留文件名和outFile对象引用,直到命令结束。当命令结束时,引用被删除,允许python清理对象。

票数 0
EN

Stack Overflow用户

发布于 2017-01-04 06:34:22

感谢HashSplat的输入。我有一些更新,使其功能齐全:

代码语言:javascript
运行
复制
class FtpWindow(QtGui.QDialog):

def __init__(self, parent=None):
    self.fileList = QtGui.QTreeWidget()
    self.ftp = QtNetwork.QFtp(self)
    self.progressDialog = QtGui.QProgressDialog(self)
    self.progressDialog.canceled.connect(self.ftp.abort)
    self.downloadAllButton.clicked.connect(self.downloadAllFile)
    self.ref_holder = {}
    self.ftp.commandFinished.connect(self.ftpCommandFinished)

def download_file(self, fileName):
    """Non blocking start downloading a file."""
    self.outFile = QtCore.QFile(fileName)

    """ Need this to have files saved locally """
    if not self.outFile.open(QtCore.QIODevice.WriteOnly):  
        QtGui.QMessageBox.information(self, "FTP",
                "Unable to save the file %s." % fileName)
        self.outFile = None
        return
    cmd_id = self.ftp.get(filename, self.outFile) # Non blocking just start downloading

    # This keeps the object alive and doesn't overwrite them.
    self.ref_holder[cmd_id] = [filename, self.outFile]

def downloadAllFile(self):
    self.progressDialog.reset()
    self.num_downloads = self.fileList.topLevelItemCount()
    self.counter=1
    self.progressDialog.setLabelText("Downloading %d/%d files ..." % (self.counter, self.num_downloads))
    self.progressDialog.show()
    for jj in range(num_downloads): # how many files in a particular folder
        fileName = self.fileList.topLevelItem(jj).text(0) 
        self.download_file(fileName) # Non blocking, and doesn't overwrite self.outFile with every iteration

def ftpCommandFinished(self, cmd_id, error=None):
    """Increased the number of items finished."""
    self.progressDialog.setValue(self.progressDialog.value()+1)
    item = self.ref_holder.pop(cmd_id) # Remove the reference for the finished item
    if error:
        self.progressDialog.setLabelText("Error downloading %s" % item[0])

    # Check if all downloads are done
    if len(self.ref_holder) == 0:
        self.progressDialog.close() # This closes the extra window
        self.outFile.close()   # You need this to have the last file saved
    else: 
        self.counter+=1
        self.progressDialog.setLabelText("Downloading %d/%d files ..." % (self.counter, self.num_downloads))

def updateDataTransferProgress(self, readBytes, totalBytes):
    self.progressDialog.setMaximum(totalBytes)
    self.progressDialog.setValue(readBytes)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41343698

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档