网站首页 > 技术文章 正文
什么是 PEX?
PEX 是 Python 可执行文件的缩写,是一个将 Python 包打包成名为 pex 文件的工具。这些可执行文件包含 Python 包和一些引导代码。例如,我们可以将 dagster 包和其依赖项打包成单个文件,然后运行它:
% pex dagster --python=python3.8 -o dagster.pex
% ./dagster.pex
Python 3.8.16 (default, Dec 7 2022, 01:24:57)
[Clang 14.0.0 (clang-1400.0.29.202)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> import dagster
>>>
复制代码
将整个环境存储在单个文件中非常方便,可以轻松地将其传输到 S3 中进行存储。PEX 提供了更多功能,不仅仅是“文件中的虚拟环境” - 这里是我们使用的其他功能:
- 隔离性
在运行时,pex 环境与其他全局包完全隔离。在环境中只有捆绑在 pex 文件中的包。我们将多个 pex 文件一起发送到同一台机器上,而不必担心环境隔离问题。
- 确定性
使用相同的输入包会生成完全相同的 pex 文件:
$ pex dagster pandas -o out.pex | sha256sum
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 -
$ pex dagster pandas -o out.pex | sha256sum
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 -
复制代码
这让我们可以使用内容寻址来识别这些 pex 文件,从而对实现可重复性更有信心。为了实现可重复性,除了使用 Docker 镜像哈希之外,我们还使用 pex 文件哈希。
- 组合
多个 pex 文件可以在运行时合并,有效地将多个环境合并为一个环境。
% pex pandas -o pandas.pex
% pex dagster -o dagster.pex
% PEX_PATH=pandas.pex ./dagster.pex
Python 3.8.16 (default, Dec 7 2022, 01:24:57)
[Clang 14.0.0 (clang-1400.0.29.202)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> import pandas
>>> import dagster
>>>
复制代码
我们使用这个功能将代码分成两个部分,在运行时合并起来:一个包含所有依赖项的 deps.pex 文件,和一个仅包含用户代码的 source.pex 文件。
- 跨平台构建
我们在 Serverless Cloud 中使用 Linux python:* -slim 衍生的基础镜像。只要有包的 wheel,pex 工具就可以在任何平台上为 Linux 构建 pex 文件。
快速部署
使用 pex 和 S3 存储 pex 文件,我们构建了一个系统,其中快速路径避免了构建和启动 Docker 镜像的开销。
我们的系统工作方式如下:当你将代码提交到 GitHub 时,GitHub 操作根据你的依赖关系是否与上一次部署不同,执行全量构建或快速构建。我们跟踪在 setup.py 和 requirements.txt 中指定的依赖关系集。
对于全量构建,我们将你的项目依赖项构建为 deps.pex 文件,将你的代码构建为 source.pex 文件。这两个文件都会上传到 Dagster Cloud。对于快速构建,我们只构建并上传 source.pex 文件。
猜你喜欢
- 2025-01-24 python时间操作,最全封装,各种年月日加减、转换、获取
- 2025-01-24 Python 打包与发布:setuptools 和 wheel 的全攻略
- 2025-01-24 [Python办公]Python脚本如何最小化打包成 .exe 文件
- 2025-01-24 将python打包成exe的方式(python程序如何打包生成exe文件)
- 2025-01-24 Python运维常用的20个库(python运维项目)
- 2025-01-24 python打包exe与源码保护(python打包程序exe)
- 2025-01-24 python程序打包成.exe执行文件,去掉多余文件,减小体积
- 2025-01-24 python打包exe指南来了,pyinstaller打包教程
- 2025-01-24 python打包exe,各种bug处理,以及解决方案
- 2025-01-24 Python—部署打包工具(python打包配置文件)
- 258℃Python短文,Python中的嵌套条件语句(六)
- 257℃python笔记:for循环嵌套。end=""的作用,图形打印
- 256℃PythonNet:实现Python与.Net代码相互调用!
- 251℃Python操作Sqlserver数据库(多库同时异步执行:增删改查)
- 251℃Python实现字符串小写转大写并写入文件
- 106℃原来2025是完美的平方年,一起探索六种平方的算吧
- 91℃Python 和 JavaScript 终于联姻了!PythonMonkey 要火?
- 83℃Ollama v0.4.5-v0.4.7 更新集合:Ollama Python 库改进、新模型支持
- 最近发表
- 标签列表
-
- python中类 (31)
- python 迭代 (34)
- python 小写 (35)
- python怎么输出 (33)
- python 日志 (35)
- python语音 (31)
- python 工程师 (34)
- python3 安装 (31)
- python音乐 (31)
- 安卓 python (32)
- python 小游戏 (32)
- python 安卓 (31)
- python聚类 (34)
- python向量 (31)
- python大全 (31)
- python次方 (33)
- python桌面 (32)
- python总结 (34)
- python浏览器 (32)
- python 请求 (32)
- python 前端 (32)
- python验证码 (33)
- python 题目 (32)
- python 文件写 (33)
- python中的用法 (32)