首页 > 解决方案 > pysftp + py7zr 解压缩挂在存档上

问题描述

一点上下文:客户端将 7-zip 存档放在远程 sftp 服务器上,我处理它们。

我的问题是,在某些 7-zip 文件上,我的程序挂起,解压缩功能永无止境,但存档的所有文件都在本地服务器上找到(存在)。

我设法在终端上通过 ctrl+c 获得堆栈跟踪:

[终端画面][1][1]:https://i.stack.imgur.com/O62U4.png

我的代码:

    def download_from_sftp(self):
    with pysftp.Connection(host=self.hostname, port=self.port, username=self.user, password=self.password, cnopts=self.cnopts) as sftp:
        self.logger.debug("Connection succesfully established ... ")

        sftp.cwd(self.path)  # Switch to a remote directory

        directory_structure = sftp.listdir_attr()

        self.logger.debug("Downloading zip files :")
        for attr in directory_structure:
            self.logger.debug(attr.filename + " " + str(attr))
            sftp.get(attr.filename, self.path_retour + attr.filename)

            with py7zr.SevenZipFile(self.path_retour + attr.filename, mode='r') as z:
                z.extractall(self.path_retour)

            os.rename(self.path_retour + attr.filename, self.path_archive_python + attr.filename)  # move zip to archive folder on local server
            sftp.remove(attr.filename)  # delete zip on remote server

对于 1000 个 7zip 存档(大多数存档小于 1mb),此问题可能会发生 1 次。我试图验证档案的完整性并且它们是有效的。在我的桌面上,py7zr 能够提取档案的所有文件而不会崩溃/挂起。

我在想也许是 sftp 连接导致挂起。

谢谢

- - 编辑 - -

根据 MartinPrikryl 的反馈,我确实在本地计算机上运行了我的整个脚本并且它没有挂起。它仅与运行脚本的服务器一起挂起该特定存档。我注意到存档比另一个大得多(~ 9mb)。但是服务器有很多磁盘空间(1 TB 可用)、4gb 内存和 4 个 CPU,所以这应该不是问题。

标签: python7zippysftppy7zlib

解决方案


我没能找到为什么 py7zr 挂在一些档案上,但是将 python 3.7.4 更新到 python 3.8.5解决了这个问题。


推荐阅读