【问题标题】:Is reading from Python zipfile thread-safe?从 Python zipfile 读取是线程安全的吗?
【发布时间】:2021-03-19 00:33:46
【问题描述】:

我看到了几个不同的意见。

我在latest docs (3.9.2) 中看不到任何内容。

我可以安全地读取 ZipFile 中的多个不同条目吗?

我看到了一些不寻常的错误,例如“解压缩数据时出现错误 -3:无效的存储块长度”,我想知道它们是否是因为我正在并行读取条目。

编辑:请不要将其作为 Is python zipfile thread-safe? 的副本关闭。如果你只看标题,你会认为它是重复的。但是,如果您阅读实际问题,它会询问 writing zip 文件(即使编写 zip 文件本质上并不是真正可并行化的)。这个问题询问阅读 zip 文件。

【问题讨论】:

    标签: python parallel-processing zipfile


    【解决方案1】:

    从表面上看,至少它计划是线程安全的:实际数据 I/O 通过_SharedFile 对象,该对象使用ZipFile 级别lock 进行读取,保持私有位置为自己:

    def read(self, n=-1):
        with self._lock:
            if self._writing():
                raise ValueError("Can't read from the ZIP file while there "
                        "is an open writing handle on it. "
                        "Close the writing handle before trying to read.")
            self._file.seek(self._pos)
            data = self._file.read(n)
            self._pos = self._file.tell()
            return data
    

    您可以尝试查看ZipFile 中的_seekable,但通常是True

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-09-13
      • 1970-01-01
      • 2011-11-27
      • 1970-01-01
      • 2011-07-19
      • 1970-01-01
      相关资源
      最近更新 更多