Python 如何从pycallgraph跟踪中获取有用信息?

Python 如何从pycallgraph跟踪中获取有用信息?,python,gephi,pycallgraph,Python,Gephi,Pycallgraph,在我的关于跟踪python执行的文章中,有人建议我使用它,因此我决定尝试以下代码: #!/bin/env python import matplotlib.pyplot as plt import numpy as np x = np.linspace(0, 2, 100) # The first call to plt.plot will automatically create # the necessary figure and axes to achieve the desired

在我的关于跟踪python执行的文章中,有人建议我使用它,因此我决定尝试以下代码:

#!/bin/env python

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 2, 100)

# The first call to plt.plot will automatically create
# the necessary figure and axes to achieve the desired plot.
plt.plot(x, x, label='linear')

# Subsequent calls to plt.plot re-use
# the current axes and each add another line.
plt.plot(x, x**2, label='quadratic')
plt.plot(x, x**3, label='cubic')

# Setting the title, legend, and axis labels also automatically
# use the current axes and set the title,
# create the legend, and label the axis respectively.
plt.xlabel('x label')
plt.ylabel('y label')

plt.title("Simple Plot")

plt.legend()

plt.show()
这段代码来自我的另一篇文章,我一直在问matplotlib中的层次结构,我得到了以下答案:

如果你真的很想看看自动创建是如何完成的,就这些了 开源。可以看到对plot()的调用创建了一个Axes实例 通过在这里的代码中调用gca()。这又调用gcf(),它 查找一个FigureManager(这是实际维护 国家)。如果存在,则返回正在管理的图形,否则返回 它使用plt.figure()创建一个新的。同样,这一过程对某些人来说也是如此 degree继承自matlab,其中初始调用通常是figure 在任何绘图操作之前

首先,我尝试使用graphviz选项的pycallgraph,但它给出了以下错误:

$ pycallgraph graphviz -- matplotlib.py
libpath/shortest.c:324: triangulation failed
libpath/shortest.c:192: source point not in any triangle
Error: in routesplines, Pshortestpath failed
Segmentation fault
Traceback (most recent call last):
  File "/usr/local/bin/pycallgraph", line 26, in <module>
    exec(__file_content)
  File "/usr/local/lib/python2.7/dist-packages/pycallgraph/pycallgraph.py", line 38, in __exit__
    self.done()
  File "/usr/local/lib/python2.7/dist-packages/pycallgraph/pycallgraph.py", line 81, in done
    self.stop()
  File "/usr/local/lib/python2.7/dist-packages/pycallgraph/pycallgraph.py", line 90, in generate
    output.done()
  File "/usr/local/lib/python2.7/dist-packages/pycallgraph/output/graphviz.py", line 112, in done
    'code %(ret)i.' % locals())
pycallgraph.exceptions.PyCallGraphException: The command "dot -Tpng -opycallgraph.png /tmp/tmpObsZGK" failed with error code 35584.
当我在gephi中打开它时,我得到了一个巨大的图(节点:1062,边:1362),它几乎是无用的,我在这里看不到任何有用的东西。因此,我尝试限制输出:

$ pycallgraph --max-depth 5 gephi -- traceme.py
这给了我一个有254个节点和251条边的图,这基本上也是无用的,因为我仍然看不到任何有用的东西。因此,我决定尝试以下内容:

egrep -i 'gcf|gca|plot' pycallgraph.gdf
但它什么也没给我。然后我开始想知道pycallgraph跟踪到底是什么,我制作了这个hello world:

#!/bin/env python
print "This line will be printed."
我用(graphviz)运行它:

输出(从生成的png文件读取)为:

\uuuuu main\uuuuu-->
  • 我可以使用什么工具来获得我提到的类似答案?换句话说,我怎么知道调用函数的顺序是:plot()->gca()->gcf()
  • pycallgraph实际上在跟踪什么
  • 为什么hello world示例使用graphviz选项,而更复杂的示例仅使用gephi选项
  • 如何保留pycallgraph生成并传递给graphviz的点格式

  • 似乎您需要程序执行的受控堆栈跟踪。这就是回溯模块的用途。请参阅。@TrisNefzger感谢您的回复,因为我可以看到
    回溯
    模块主要用于错误处理还是我错了?我发现
    trace
    模块与我想要的模块非常接近,只是我无法确定调用哪个函数,而且也没有参数。Traceback具有处理堆栈的功能,没有异常。在“使用堆栈”一节中有一个关于此的简短教程,介绍了其print_Stack()、format_Stack()和extract_Stack()函数的使用。这是一种奇怪的方法。如果查看提供的示例,您将看到
    call\u函数
    (正在跟踪的函数)需要将回溯函数(正在跟踪的函数)作为第一个参数。这意味着当我想要跟踪某个模块时,我必须覆盖它的函数
    #!/bin/env python
    print "This line will be printed."
    
    pycallgraph graphviz -- hello_world.py
    
    __main__ ---> <module>