“Python 3.15 的 profiling.sampling 在 macOS 上报 Permission Error: Unable to access process memory” 是新采样分析器第一次运行最容易撞上的错,而且 cProfile 的实现已经搬进了 profiling.tracing。这篇在本机用 3.14.9 和 3.15.0 并排看包结构、消失的名字、报告格式,以及分析的代价;采样分析器本身因为需要 sudo 没有测。
Python 3.14(退出码 0):
import profiling -> ModuleNotFoundError: No module named 'profiling'
cProfile.Profile.__module__ = cProfile
profiling.tracing -> ModuleNotFoundError: No module named 'profiling'
Python 3.15(退出码 0):
import profiling -> profiling | 子包: ['sampling', 'tracing']
cProfile.Profile.__module__ = profiling.tracing
cProfile.Profile is profiling.tracing.Profile = True
cProfile.run is profiling.tracing.run = True3.15 新增 profiling 包;cProfile 变成指向 profiling.tracing 的兼容层。
Python 3.14(退出码 0):
hasattr(cProfile, 'main') = True
hasattr(cProfile, 'label') = True
hasattr(cProfile, 'io') = True
hasattr(cProfile, 'importlib') = True
Usage: cProfile.py [-o output_file_path] [-s sort] [-m module | scriptfile] [arg] ...
cProfile.main() -> SystemExit: 2
Python 3.15(退出码 0):
hasattr(cProfile, 'main') = False
hasattr(cProfile, 'label') = False
hasattr(cProfile, 'io') = False
hasattr(cProfile, 'importlib') = False
cProfile.main() -> AttributeError: module 'cProfile' has no attribute 'main'
from cProfile import label -> ImportError: cannot import name 'label' from 'cProfile' (<py3.15>/lib/python3.15/cProfile.py)
Python 3.14:python -m cProfile -s tottime w.py(退出码 0)——前几行:
109469 function calls (19 primitive calls) in 0.291 seconds
Ordered by: internal time
ncalls tottime percall cumtime percall filename:lineno(function)
Python 3.15:python -m cProfile -s tottime w.py(退出码 0)——前几行:
109469 function calls (19 primitive calls) in 0.284 seconds
Ordered by: internal time
ncalls tottime percall cumtime percall filename:lineno(function)cProfile 模块里的 main、label、io、importlib 在 3.15 里没有了;命令行用法不变。
Python 3.14(退出码 0):
109469 function calls (19 primitive calls)
1 0.000 0.000 0.302 0.302 {built-in method builtins.exec}
1 0.000 0.000 0.302 0.302 w.py:1(<module>)
1 0.000 0.000 0.302 0.302 w.py:5(main)
5 0.000 0.000 0.273 0.055 w.py:4(io)
109455/5 0.029 0.000 0.029 0.006 w.py:3(fib)
Python 3.15(退出码 0):
109469 function calls (19 primitive calls)
1 0.000 0.000 0.288 0.288 {built-in method builtins.exec}
1 0.000 0.000 0.288 0.288 w.py:1(<module>)
1 0.000 0.000 0.288 0.288 w.py:5(main)
5 0.000 0.000 0.272 0.054 w.py:4(io)
109455/5 0.016 0.000 0.016 0.003 w.py:3(fib)同一个脚本的 cProfile 报告在两个版本上结构一样,只有耗时不同。
python3.15 -m profiling.sampling --help(子命令部分):
positional arguments:
{run,attach,dump,replay}
Command to run
run Run and profile a script or module
attach Attach to and profile a running process
dump Dump a running process's current stack
replay Replay a binary profile and convert to another format
run 的采样模式 --mode: wall,cpu,gil,exception
run 的输出格式选项: --pstats, --collapsed, --flamegraph, --gecko, --heatmap, --jsonl, --binary | 另有 --diff-flamegraph BASELINE
默认采样率(帮助里写的): 1khz
python3.15 -m profiling.sampling run w.py(退出码 1):
🔒 Permission Error: Unable to access process memory on macOS
Tachyon needs elevated permissions to profile processes. Try one of these solutions:
python3.15 -m profiling.sampling attach <某个 pid>(退出码 1):
🔒 Permission Error: Unable to access process memory on macOS
Tachyon needs elevated permissions to profile processes. Try one of these solutions:
python3.15 -m profiling.sampling dump <某个 pid>(退出码 1):
🔒 Permission Error: Unable to access process memory on macOS
Tachyon needs elevated permissions to profile processes. Try one of these solutions:profiling.sampling 有 run、attach、dump、replay;macOS 上不加 sudo 三个命令都是 Permission Error。
Python 3.14:不分析 3.07 ms,cProfile 分析 20.64 ms Python 3.15:不分析 3.83 ms,cProfile 分析 20.70 ms Python 3.15 的 pstats 读 Python 3.14 写的 .prof(退出码 0):150050 function calls (2 primitive calls) Python 3.14 的 pstats 读 Python 3.15 写的 .prof(退出码 0):150050 function calls (2 primitive calls)
确定性分析把同一段代码拖慢好几倍;.prof 文件在 3.14 和 3.15 之间能互读。