[英]Python cProfile - decorated functions obscuring profile visualization
I have a base class with a @classmethod
which acts as a decorator for a large number of methods in many descendant classes.我有一个带有
@classmethod
的基类,它充当许多后代类中大量方法的装饰器。
class BaseClass():
@classmethod
def some_decorator(cls, method):
@wraps(method)
def inner_method(self, *args, **kwargs):
# do stuff
return method(self, *args, **kwargs)
return inner_method
class ChildClass(BaseClass):
@BaseClass.some_decorator
def some_child_method(self):
# do other stuff
return
When I profile this code and look at it through a tree view, I see thousands of calls to some_decorator
from hundreds of different places.当我剖析此代码并通过树视图查看它时,我看到来自数百个不同位置的数千次对
some_decorator
的调用。
And then I see some_decorator
call back out to hundreds of the places it just came from.然后我看到
some_decorator
回调到它刚刚来自的数百个地方。
It's quite annoying and I have yet to figure out a way around it, neither through changing the code nor profiling a different way.这很烦人,我还没有找到解决方法,既不是通过更改代码也不是通过不同的方式进行分析。 (Using gprof2dot atm: How can you get the call tree with python profilers? )
(使用 gprof2dot atm: 如何使用 python 分析器获取调用树? )
Thoughts?想法?
There are ways to build decorators to preserve docs/signatures.有一些方法可以构建装饰器来保存文档/签名。 The wrapt library provides a lot of functionality to that end.
wrapt 库为此提供了很多功能。
https://wrapt.readthedocs.io/en/latest/decorators.html#decorating-class-methods https://wrapt.readthedocs.io/en/latest/decorators.html#decorating-class-methods
It would end up looking something like this:它最终看起来像这样:
class BaseClass():
@wrapt.decorator
@classmethod
def some_decorator(cls, method, instance, *args, *kwargs):
# do stuff
return method(instance, *args, **kwargs)
I have a base class with a @classmethod
which acts as a decorator for a large number of methods in many descendant classes.我有一个带有
@classmethod
的基类,它充当许多后代类中大量方法的装饰器。
class BaseClass():
@classmethod
def some_decorator(cls, method):
@wraps(method)
def inner_method(self, *args, **kwargs):
# do stuff
return method(self, *args, **kwargs)
return inner_method
class ChildClass(BaseClass):
@BaseClass.some_decorator
def some_child_method(self):
# do other stuff
return
When I profile this code and look at it through a tree view, I see thousands of calls to some_decorator
from hundreds of different places.当我分析这段代码并通过树视图查看它时,我看到从数百个不同的地方对
some_decorator
数千次调用。
And then I see some_decorator
call back out to hundreds of the places it just came from.然后我看到
some_decorator
回调它刚刚来自的数百个地方。
It's quite annoying and I have yet to figure out a way around it, neither through changing the code nor profiling a different way.这很烦人,我还没有找到解决方法,既不是通过更改代码也不是通过不同的方式进行分析。 (Using gprof2dot atm: How can you get the call tree with python profilers? )
(使用 gprof2dot atm: 如何使用 python 分析器获取调用树? )
Thoughts?想法?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.