-
py-spy is a sampling profiler for Python programs.
-
It lets you visualize what your Python program is spending time on without restarting the program or modifying the code in any way.
-
py-spy is extremely low overhead; doesn’t run in the same process as the profiled Python program.
-
py-spy is safe to use against production Python code.
Command | What it does | Example |
---|---|---|
record | Collects a fixed-duration sample set and writes it to SVG (flame-graph), Speedscope JSON, or Chrome trace | py-spy record -o profile.svg --pid 12345 |
top | Live “top-like” view, refreshed every second | py-spy top -- python app.py |
dump | One-off stack traces (optionally with locals) | py-spy dump --pid 12345 --locals |
handy flags:
--full-filenames
(Show full Python filenames, instead of shortening to show only the package partfi)record -f speedscope -o profile.json
(load in https://www.speedscope.app)--rate
(sampling frequency),--gil
(only threads holding the GIL),--native
(include C/C++ frames on x86-64)--subprocesses
, (follow forks/spawns)