gpt4 book ai didi

python - django 中的 PyCallGraph 中间件

转载 作者:太空宇宙 更新时间:2023-11-03 17:46:02 26 4
gpt4 key购买 nike

我正在尝试在 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' 对象没有属性 'pycallgraph'.这怎么可能?显然这条线

self.pycallgraph = pycallgraph
不考虑

。为什么?

最佳答案

我确实忘记导入 GlobbingFilter 所以我遇到了一个异常,不会让代码运行到 self.pycallgraph = pycallgraph

PyCharm 也未正确配置。我解决了,感谢这个答案:
https://stackoverflow.com/a/20335280/1191416

关于python - django 中的 PyCallGraph 中间件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29776107/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com