Skip to main content
 首页 » 编程设计

python之查找 python 内置函数的调用者

2024年11月01日9jiqing9006

这类似于 Profiling in Python: Who called the function? 但有一个转折。我有一个 python 函数,我正在使用 cProfile 进行分析。在我第一次运行时,统计数据显示它大部分时间都花在 datetimes.py 的 get_loc() 中;

好吧,我在我的系统上找到了datetimes.py文件,并在顶部临时添加了import pdb和pdb.set_trace() get_loc() 然后随机使用调试器命令 where 几次,在 之间随机使用 continue >在哪里。这向我表明,根据我的随机样本,几乎所有对 get_loc() 的调用最终都来 self 自己代码中的同一个地方。我修改了该代码以显着减少调用次数。太棒了!

接下来,我现在看到大量时间花费在 超过 66,000 次 builtins.isinstance() 调用上;问题是我找不到源代码来实现堆栈跟踪以找出调用 isinstance() 的内容。 (我猜测,基于我对 Python 的有限了解,isinstance 实际上是直接或通过共享对象链接到 Python 解释器的 C 代码)。无论如何,我找不到正在调用的是 isinstance。我尝试了 Gprof2dot 方法,也在 Profiling in Python: Who called the function? 的答案中找到了但这只告诉我 isinstance() 正在被 is_dtype() 调用并且调用图停在那里(在 is_dtype) .

有什么想法吗?

请您参考如下方法:

我已经做了一些挖掘工作,并将尝试至少部分地回答我的问题(希望其他人能够看到并提供更多关于如何简化此问题的想法)。

下面是我如何分析我的函数

pr = cProfile.Profile()                # create a cProfiler object  
pr.enable()                            # turn profiling on 
pr.runcall( myfunc, arg1, arg2, ... )  # profile my function 
pr.disable()                           # turn profiling off 

那么,这里是寻找来电者的关键:

import pstats 
p = pstats.Stats(pr)           # create pstats obj based on profiler above. 
p.print_callers('isinstance')  # find all the callers of isinstance. 

print_callers 函数打印出 130 个 isinstance 调用者,以及每个调用者调用 isinstance 的次数。要消化的内容很多,但出于分析目的,将重点放在调用次数最多的少数人身上并找到他们的调用者是有意义的。这是列表的一部分(print_callers 以或多或少的随机顺序显示,但我已根据调用次数对它们进行了排序)...

Function {built-in method builtins.isinstance} was called by... 
                                           ncalls  tottime  cumtime 
                                             2808    0.002    0.002  /anaconda3/lib/python3.7/site-packages/pandas/core/indexes/datetimes.py:974(get_loc) 
                                             2158    0.006    0.018 /anaconda3/lib/python3.7/site-packages/pandas/core/dtypes/base.py:75(is_dtype) 
                                             2030    0.001    0.001  /anaconda3/lib/python3.7/site-packages/pandas/core/dtypes/common.py:1845(_is_dtype_type) 
                                             1930    0.001    0.001  /anaconda3/lib/python3.7/site-packages/pandas/core/dtypes/common.py:1981(pandas_dtype) 
                                             1440    0.001    0.001  /anaconda3/lib/python3.7/site-packages/pandas/core/dtypes/dtypes.py:68(find) 
                                             1058    0.001    0.001  /anaconda3/lib/python3.7/site-packages/pandas/core/indexes/datetimes.py:924(get_value) 
                                              841    0.000    0.000  /anaconda3/lib/python3.7/site-packages/pandas/core/dtypes/common.py:1809(_get_dtype) 
                                              726    0.000    0.000  /anaconda3/lib/python3.7/site-packages/pandas/core/dtypes/common.py:1702(is_extension_arr 
... 
                                                6    0.000    0.000  /anaconda3/lib/python3.7/site-packages/pandas/core/indexing.py:153(_get_setitem_indexer) 
                                                5    0.000    0.000  /anaconda3/lib/python3.7/site-packages/pandas/core/ops.py:1660(wrapper) 
                                                5    0.000    0.000  /anaconda3/lib/python3.7/site-packages/pandas/core/ops.py:1447(_align_method_SERIES) 
                                                5    0.000    0.000 /anaconda3/lib/python3.7/site-packages/pandas/core/computation/expressions.py:163(_has_bool_dtype) 
                                                4    0.000    0.000 /anaconda3/lib/python3.7/site-packages/pandas/core/internals/blocks.py:3100(_extend_blocks) 
                                                4    0.000    0.000  /anaconda3/lib/python3.7/site-packages/pandas/core/indexing.py:2501(check_setitem_lengths) 
                                                3    0.000    0.000  /anaconda3/lib/python3.7/site-packages/pandas/core/indexes/base.py:566(_shallow_copy) 
                                                3    0.000    0.000  /anaconda3/lib/python3.7/site-packages/pandas/core/dtypes/common.py:702(is_datetimelike) 
                                                3    0.000    0.000  /anaconda3/lib/python3.7/posixpath.py:41(_get_sep) 
                                                2    0.000    0.000  /anaconda3/lib/python3.7/distutils/version.py:331(_cmp) 

在这一点上,我想我可以单调乏味地浏览最大的调用者(找到他们最大的调用者,等等)或编写脚本来完成。将继续挖掘并在以后发布更多进展。