本文介绍了如何分析我的 python 应用程序的类方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去,我在同一个文件中编写了包含所有函数的 python 代码,我可以使用以下代码分析我的程序:

In the past I've written python code with all functions in the same file, and I could profile my programs using the following code:

这是我写的装饰器:

def do_profile(cond):
    def resdec(f):
        if not cond:
            return f
        return profile(f)
    return resdec

这就是我使用它的方式:

And this is how I use it:

@do_profile(DO_PROFILE)
def my_func():
    return 1

然后我会在我的脚本中调用 kernprof.py:

I would then invoke kernprof.py on my script:

kernprof.py  -l my_program.py

与此同时,我对 OOP 更加熟悉,我将程序重写为许多类,现在程序是这样启动的:

In the meantime I got more familiar with OOP and I rewrote my program into many classes and the program is now started like this:

 if __name__ == "__main__":
     my_app = myApp()
     my_app.run()

myApp 是一个也与其他类进行大量通信的类:

myApp is a class which is also communicating heavily with other classes:

class myApp():
    @do_profile(DO_PROFILE)
    def foo_method(self, arg1):
        pass

我在每个 myApp 方法前面添加了 do_profile 装饰器,但是如果我运行 kernprof.py,结果 .prof 文件为空

I've added the do_profile decorator in front of each myApp method, but if I run kernprof.py, the resulting .prof file is empty

那么分析类的方法的最简单方法是什么?我真的很想用装饰器和标志来打开/关闭它.

So what's the easiest way of profiling a methods of a class? I would really love to switch this on / off with a decorator and a flag.

我对这里最简单的解决方案很感兴趣.找到一个装饰器是一个优雅的解决方案,但也许事情可以更容易地完成.我不想做的是使用诸如 cProfile's profile profile.runctx('self.baz()', globals(), locals()) 之类的东西.在处理许多类和方法时,这不是一个实用的解决方案.

I'm really interested in the easiest possible solution here. A find a decorator to be an elegant solution, but maybe things can be done easier. What I DO NOT want to do, is using stuff like cProfile's profile profile.runctx('self.baz()', globals(), locals()) . That is not a practical solution when handling many classes and methods.

推荐答案

profile 函数本身就是一个装饰器,与大多数装饰器一样,它们只需要应用于函数.

The profile function is a decorator itself, and like most decorators, they need to be applied to functions only.

幸运的是,类方法基本上是在创建实例时绑定到实例的函数.因此,您可以通过方法本身将装饰器放入类定义中,将装饰器应用于任何类方法:

Luckily, class methods are basically functions that are bound to an instance when an instance is created. Thus, you can apply your decorator to any class method by putting it in the class definition by the methods themselves:

class myApp(object):
    @do_profile(DO_PROFILE)
    def foo_method(self, arg1):
        pass

    @do_profile(DO_PROFILE)
    def bar_method(self, arg2):
        pass

如果您使用 python 2.6 或更高版本,您还可以创建一个类装饰器 并将 profile 装饰器应用于任何给定类的所有方法.您可以通过将装饰器放在类定义之前来应用它:

If you use python 2.6 or up, you can also create a class decorator and apply the profile decorator to all methods on any given class. You'd apply it by placing the decorator right before the class definition:

@do_profile_all_methods(DO_PROFILE)
class myApp(object):
    def foo_method(self):
        pass

这样的装饰器看起来像这样:

Such a decorator could look something like this:

import types

def do_profile_all_methods(cond):
    if not cond:
        return lambda c: c # Do nothing with the class; the 'null' decorator
    def profile_all_methods(klass):
        for name, attr in klass.__dict__.items():
            if isinstance(attr, types.UnboundMethodType):
                klass[name] = profile(attr)
        return klass
    return profile_all_methods

此装饰器仅将 profile 包装器应用于直接方法,而不是从基类继承的任何方法.

This decorator only applies the profile wrapper to direct methods, not any inherited from the base class.

这篇关于如何分析我的 python 应用程序的类方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 06:51