py-spy 安装和配置指南
py-spy Sampling profiler for Python programs 项目地址: https://gitcode.com/gh_mirrors/py/py-spy
1. 项目基础介绍和主要编程语言
项目基础介绍
py-spy
是一个用于 Python 程序的采样分析器(sampling profiler)。它允许你在不重启程序或修改代码的情况下,可视化 Python 程序的运行情况。py-spy
具有极低的开销,因为它使用 Rust 编写,并且不会在与被分析的 Python 程序相同的进程中运行。这意味着 py-spy
可以安全地用于生产环境中的 Python 代码分析。
主要编程语言
py-spy
主要使用 Rust 语言编写,以确保其高性能和低开销。
2. 项目使用的关键技术和框架
关键技术
- Rust:
py-spy
的核心代码使用 Rust 编写,以确保高效和安全。 - Python: 用于分析和可视化 Python 程序的运行情况。
- CPython 解释器: 支持所有最近的 CPython 解释器版本(2.3-2.7 和 3.3-3.11)。
框架
- PyPI: 通过 PyPI 分发预构建的二进制轮子。
- Homebrew: 在 macOS 上通过 Homebrew 安装。
- AUR: 在 Arch Linux 上通过 AUR 安装。
3. 项目安装和配置的准备工作和详细安装步骤
准备工作
在安装 py-spy
之前,请确保你的系统满足以下要求:
- 操作系统: Linux, macOS, Windows 或 FreeBSD。
- Python 版本: 支持 CPython 2.3-2.7 和 3.3-3.11。
- Rust 工具链(可选): 如果你希望通过 Rust 编译安装,请确保已安装 Rust 工具链。
详细安装步骤
通过 PyPI 安装
-
安装 pip(如果尚未安装):
sudo apt-get install python3-pip # 对于 Debian/Ubuntu 系统
-
安装 py-spy:
pip install py-spy
通过 Homebrew 安装(仅限 macOS)
-
安装 Homebrew(如果尚未安装):
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
-
安装 py-spy:
brew install py-spy
通过 AUR 安装(仅限 Arch Linux)
-
安装 yay(如果尚未安装):
sudo pacman -S yay
-
安装 py-spy:
yay -S py-spy
通过 Rust 编译安装
-
安装 Rust 工具链(如果尚未安装):
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
-
安装 py-spy:
cargo install py-spy
配置和使用
安装完成后,你可以通过命令行使用 py-spy
来分析 Python 程序。以下是一些常用的命令:
-
生成火焰图:
py-spy record -o profile.svg --pid 12345
-
实时查看函数调用情况:
py-spy top --pid 12345
-
显示当前调用栈:
py-spy dump --pid 12345
通过这些步骤,你可以轻松地安装和配置 py-spy
,并开始分析你的 Python 程序。
py-spy Sampling profiler for Python programs 项目地址: https://gitcode.com/gh_mirrors/py/py-spy