Pycallgraph在调试模式下不生成图形输出

2 投票
1 回答
916 浏览
提问于 2025-04-18 12:38

我正在使用Pycallgraph来生成输出,但我想保存中间的graphd输出(而不是生成图片),因为我想对它做一些小修改。

我运行的命令是:

PYTHONPATH=. pycallgraph -d graphviz -- ./ab_ndh_graph.py > out.graphd

这个命令生成了两个东西:

  1. pycallgraph.png -- 这是整个调用图(graphd的输出在out.graphd中)
  2. filter_max_depth.png -- 这是基于代码的调用图(正确,但没有graphd的输出)

我该如何让“filter_max_depth”也生成graphd的输出呢?

文件内容:

config = Config(max_depth=2)
config.trace_filter = GlobbingFilter(exclude=[
    'pycallgraph.*',
])
graphviz = GraphvizOutput(output_file='filter_max_depth.png')

with PyCallGraph(output=graphviz, config=config):
    o = AB_NDH()
    o.run()

1 个回答

0

GraphvizOutput这个类会使用一个临时文件来保存dot源代码,然后在这个文件上运行dot这个命令行工具,最后再把这个临时文件清理掉。

不过,你可以很简单地通过在运行PyCallGraph之后调用generate()方法来重新生成相同的文件内容:

with open('filter_max_depth.graphd', 'w') as dotfile:
    dotfile.write(graphviz.generate())

你也可以在你的config对象上开启调试选项;这样的话,dot源代码会被写入日志中。要在config对象上设置.debug标志:

config.debug = True

但在命令行上就不要使用-d选项:

PYTHONPATH=. pycallgraph graphviz -- ./ab_ndh_graph.py > out.graphd

其实,这和添加print graphviz.generate()是一样的。

撰写回答