• https://github.com/benfred/py-spy

  • 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.

CommandWhat it doesExample
recordCollects a fixed-duration sample set and writes it to SVG (flame-graph), Speedscope JSON, or Chrome tracepy-spy record -o profile.svg --pid 12345
topLive “top-like” view, refreshed every secondpy-spy top -- python app.py
dumpOne-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)