代码之家  ›  专栏  ›  技术社区  ›  Paul Draper

从Python zipfile读取线程安全吗?

  •  2
  • Paul Draper  · 技术社区  · 5 年前

    在这个问题上,我看到了几个不同的观点。

    我什么也没看到 latest docs (3.9.2).

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

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

    编辑:请不要将其作为副本关闭 Is python zipfile thread-safe? .如果你只看标题,你会认为它是重复的。但如果你读了实际问题,它会问 zip文件(尽管编写zip文件本身并不真正可并行)。这个问题问的是 阅读 压缩文件。

    0 回复  |  直到 5 年前
        1
  •  0
  •   tevemadar    5 年前

    从外观上看,至少它计划是线程安全的:实际的数据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
    

    你可以试试看 _seekable 关于 ZipFile ,但通常情况下 True .

    推荐文章