ワンクリックで
py-spy
Diagnose Python performance problems with py-spy using safe profiling workflows, stack dumps, and flamegraph interpretation.
Codex または Claude でインストール この Prompt をコピーして Codex、Claude、または他のアシスタントに貼り付けると、Skill ページを確認してインストールできます。
メニュー
Diagnose Python performance problems with py-spy using safe profiling workflows, stack dumps, and flamegraph interpretation.
Codex または Claude でインストール この Prompt をコピーして Codex、Claude、または他のアシスタントに貼り付けると、Skill ページを確認してインストールできます。
SOC 職業分類に基づく
| name | py-spy |
| description | Diagnose Python performance problems with py-spy using safe profiling workflows, stack dumps, and flamegraph interpretation. |
Use this skill when the user needs to diagnose Python runtime behavior with py-spy, especially:
py-spy top, record, or dump outputThis skill wraps the upstream py-spy CLI. It does not modify py-spy source code.
docs/bottleneck-decision-tree.md before choosing a command.py-spydump for hangs, waits, deadlocks, or low-CPU slownesstop for live CPU hotspotsrecord for flamegraph evidencepy-spy-profile.svgpy-spy-profile.speedscope.jsonpy-spy-dump.txtprofile-analysis.mddump-analysis.md./py-spy-helper.sh doctor
Use when the user asks "what is using CPU right now?"
py-spy top --pid <PID>
Helper form:
./py-spy-helper.sh top-pid <PID>
Use when the user needs evidence that can be inspected later.
py-spy record -o profile.svg --duration 30 --pid <PID>
Helper form:
./py-spy-helper.sh record-pid <PID> profile.svg 30
Use after generating an SVG flamegraph with py-spy record. This extracts frame titles from the SVG, sorts by sample percentage, applies coarse categories, and writes a Markdown report.
./py-spy-helper.sh analyze-flamegraph profile.svg profile-analysis.md 10
Treat the generated report as triage input. The agent must still inspect the flamegraph context and apply the interpretation template before giving final advice.
Use when the process is hanging, deadlocked, waiting, or not making progress. For full scenario guidance, use docs/cookbook/hanging-process.md.
py-spy dump --pid <PID> > py-spy-dump.txt
Helper form:
./py-spy-helper.sh dump-pid <PID> py-spy-dump.txt
Use after py-spy dump to identify repeated leaf frames and common wait categories.
./py-spy-helper.sh analyze-dump py-spy-dump.txt dump-analysis.md 10
Use this for:
The generated report is a snapshot-based triage result, not proof of a persistent bottleneck.
Use when attaching to an existing process is blocked by permissions.
py-spy record -o profile.svg --duration 30 -- python app.py
Helper form:
./py-spy-helper.sh record-cmd profile.svg -- python app.py
Use for gunicorn, celery, multiprocessing, or worker pools.
py-spy record --subprocesses -o profile.svg --duration 30 --pid <PID>
The helper supports this via an environment variable:
PY_SPY_SUBPROCESSES=1 ./py-spy-helper.sh record-pid <PID> profile.svg 30
Always follow these rules:
sudo./proc/sys/kernel/yama/ptrace_scope.SYS_PTRACE capability.securityContext.py-spy dump --locals unless the user explicitly confirms, because local variables may contain secrets.When a privileged command is necessary, present it as a suggestion and ask for explicit confirmation before the user runs it.
Before making a recommendation, use docs/bottleneck-decision-tree.md to decide whether the symptom fits CPU profiling, dump analysis, DB tracing, network tracing, memory tools, or another follow-up.
When the user provides a flamegraph, speedscope profile, raw sample output, or screenshot of a flamegraph, use docs/flamegraph-interpretation-template.md as the report structure.
For SVG flamegraphs, first run this when the file is available in the workspace:
./py-spy-helper.sh analyze-flamegraph <INPUT.svg> <OUTPUT.md> 10
Then combine the generated report with visual inspection of the flamegraph.
Core reading rules:
dumpUse dump for hangs, low-CPU slowness, waiting threads, and suspected lock contention. When a dump file is available, run:
./py-spy-helper.sh analyze-dump <INPUT.txt> <OUTPUT.md> 10
Explain:
结论:<one-sentence main finding>
关键路径 / 重复栈:
`<root> -> <function> -> <hotspot-or-leaf>`
判断:这是 <CPU / IO / DB / lock / native / GIL / memory / inconclusive> 问题,置信度 <low / medium / high>。
证据:
- <evidence 1>
- <evidence 2>
下一步:
1. <safe next step>
2. <optional deeper profile or verification>
topUse top as a quick live signal. Explain:
record run is neededExplain that attaching to an existing process may require elevated privileges or ptrace permission. Do not run elevated commands automatically.
Safer alternatives:
py-spy instead of attachingIf py-spy runs inside a container and cannot read process memory, Docker may need SYS_PTRACE. Do not modify container config automatically.
If profiling inside Kubernetes, an ephemeral container or security context may be needed. Explain the tradeoff and ask for confirmation before suggesting changes.
For C, C++, Cython, NumPy, or other native-heavy workloads, suggest --native only when it is supported on the platform and symbols are available.
If CPU is not high and dump/flamegraph results are inconclusive while latency grows over time, py-spy may be insufficient. Suggest Python memory tools such as tracemalloc, memray, object growth analysis, or GC logging.
py-spypython3speedscope viewer for .speedscope.json filespy-spy-helper.shscripts/analyze-flamegraph.pyscripts/analyze-dump.pydocs/bottleneck-decision-tree.mddocs/flamegraph-interpretation-template.mddocs/cookbook/high-cpu.mddocs/cookbook/hanging-process.mdscripts/smoke-py-spy-skill.sh