我正在尝试在django(1.4)中实现中间件以使用PyCallGraph创建调用图。
我从网上找到的两个不同片段中得出了这一点。看起来是这样的:

import time
from django.conf import settings
from pycallgraph import Config
from pycallgraph import PyCallGraph
from pycallgraph.output import GraphvizOutput

class CallgraphMiddleware(object):
    def process_view(self, request, callback, callback_args, callback_kwargs):
        if settings.DEBUG and 'graph' in request.GET:
            config = Config()
            config.trace_filter = GlobbingFilter(exclude=['pycallgraph.*','*.secret_function',], include=['reports.*'])
            graphviz = GraphvizOutput(output_file='callgraph-' + str(time.time()) + '.png')
            pycallgraph = PyCallGraph(output=graphviz, config=config)
            pycallgraph.start()
            self.pycallgraph = pycallgraph

    def process_response(self, request, response):
        if settings.DEBUG and 'graph' in request.GET:
            self.pycallgraph.done()
        return response


我已将其添加到settings.py上安装的其他中间件中,然后启动了服务器。
似乎在process_view被调用时触发,但是在到达process_response时django抱怨,告诉我'CallgraphMiddleware' object has no attribute 'pycallgraph'。那怎么可能?显然线

self.pycallgraph = pycallgraph


不考虑在内。为什么?

最佳答案

我确实忘记了导入GlobbingFilter,所以我有一个例外,即不允许代码运行到self.pycallgraph = pycallgraph

另外,PyCharm没有正确配置。我解决了这个答案:
https://stackoverflow.com/a/20335280/1191416

10-07 17:43