我试图在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没有被考虑在内。为什么?
发布于 2015-04-21 15:41:50
我确实忘了导入GlobbingFilter,所以我有一个不允许代码一直运行到self.pycallgraph = pycallgraph行的异常
另外,PyCharm没有被正确配置。我解决了这个问题,感谢这个答案:
https://stackoverflow.com/questions/29776107
复制相似问题