打包 Python 项目时将 zip_safe 设置为 True 有什么好处? [英] What is the advantage of setting zip_safe to True when packaging a Python project?

查看:94
本文介绍了打包 Python 项目时将 zip_safe 设置为 True 有什么好处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

setuptools 文档仅说明:

The setuptools documentation only states:

为了获得最佳性能,最好将 Python 包安装为 zip 文件.然而,并非所有软件包都能够以压缩形式运行,因为它们可能希望能够像正常的操作系统文件一样访问源代码或数据文件.因此,setuptools 可以将您的项目安装为 zip 文件或目录,其默认选择由项目的 zip_safe 标志决定(参考).

For maximum performance, Python packages are best installed as zip files. Not all packages, however, are capable of running in compressed form, because they may expect to be able to access either source code or data files as normal operating system files. So, setuptools can install your project as a zipfile or a directory, and its default choice is determined by the project's zip_safe flag (reference).

实际上,获得的性能优势是什么?是否值得调查我的项目是 zip 安全的,还是收益通常很小?

In practical terms, what is the performance benefit gained? Is it worth investigating if my projects are zip-safe, or are the benefits generally minimal?

推荐答案

Zip 文件在磁​​盘上占用的空间更少,这也意味着它们可以更快地从磁盘读取.由于大多数东西都是 I/O 绑定的,解压缩包的开销可能小于从磁盘读取更大文件的开销.此外,单个较小的 zip 文件可能会按顺序存储在磁盘上,而较小文件的集合可能会更加分散.在旋转媒体上,这也通过减少查找次数来提高读取性能.因此,您通常会以一些 CPU 时间为代价来优化磁盘使用,这可能会显着改善您的导入和加载时间.

Zip files take up less space on disk, which also means they're more quickly read from disk. Since most things are I/O bound, the overhead in decompressing the packaging may be less than the overhead in reading a larger file from disk. Moreover, it's likely that a single, small-ish zip file is stored sequentially on disk, while a collection of smaller files may be more spread out. On rotational media, this also increases read performance by cutting down the number of seeks. So you generally optimize your disk usage at the cost of some CPU time, which may dramatically improve your import and load times.

这篇关于打包 Python 项目时将 zip_safe 设置为 True 有什么好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆