Skip to content

Profiling with py-spy

Overview

py-spy is a sampling profiler for Python that visualizes what your code spends time on without restarting the process.

Docker Setup

Dockerfile

RUN pip install py-spy

docker-compose.yml

services:
app:
cap_add:
- SYS_PTRACE
security_opt:
- seccomp:unconfined

Usage

1. Find Process ID

Terminal window
docker exec -it cloud-document-maker ps aux
# Look for gunicorn worker process (not master)

2. Record Profile

Terminal window
# Record to SVG flamegraph
docker exec -it cloud-document-maker py-spy record \
-o /app/output/profile.svg --pid <WORKER_PID>
# Record with duration limit (60 seconds)
docker exec -it cloud-document-maker py-spy record \
-o /app/output/profile.svg --pid <WORKER_PID> --duration 60
# Record to speedscope format (interactive viewer)
docker exec -it cloud-document-maker py-spy record \
-o /app/output/profile.json --format speedscope --pid <WORKER_PID>

3. Real-time Monitoring

Terminal window
# Live top-like view of function calls
docker exec -it cloud-document-maker py-spy top --pid <WORKER_PID>

4. View Flamegraph

Open ./output/profile.svg in browser.

Reading Flamegraphs

ElementMeaning
WidthTime spent in function
HeightCall stack depth
Wide barsPerformance hotspots
ColorRandom (no meaning)

Tips:

  • Look for wide bars at the top — these are slow leaf functions
  • Click on bars to zoom in
  • Search for specific function names

Troubleshooting

ErrorSolution
Permission deniedAdd SYS_PTRACE to docker-compose
Process not foundWorker may have restarted, re-check PID

Output Formats

FormatUse Case
svgStatic flamegraph (default)
speedscopeInteractive web viewer
rawRaw sample data