Python Call Graph¶
Python Call Graph is a Python module that creates call graph visualizations for Python applications.
安装:
pip install python-call-graph
备注
详细使用参见: demo-python
参数¶
General Arguments:
-d, --debug
Turns on debug mode which will print out debugging information.
-ng, --no-groups
Do not group modules in the results.
By default this is turned on and will visually group together methods of the same module.
The technique of grouping does rely on the type of output used.
-s, --stdlib
When running a trace, also include the Python standard library.
-m, --memory
An experimental option which includes memory tracking in the trace.
-t, --threaded
An experimental option which processes the trace in another thread.
This may or may not be faster.
Filtering Arguments:
-i, --include <pattern>
Wildcard pattern of modules to include in the output.
You can have multiple include arguments.
-e, --exclude <pattern>
Wildcard pattern of modules to exclude in the output.
You can have multiple include arguments.
--include-pycallgraph
By default pycallgraph filters itself out of the trace.
Enabling this will include pycallgraph in the trace.
--max-depth
Maximum stack depth to trace.
Any calls made past this stack depth are not included in the trace.
实例¶
示例:
# Create a call graph image called pycallgraph.png on myprogram.py:
pycallgraph graphviz -- ./myprogram.py
# Create a call graph of a standard Python installation script with command line parameters:
pycallgraph graphviz --output-file=setup.png -- setup.py --dry-run install
# Run Django’s manage.py script, but since there are many calls within Django, and will cause a massively sized generated image,
# we can filter it to only trace the core Django modules:
pycallgraph -v --stdlib --include "django.core.*" graphviz -- ./manage.py syncdb --noinput
示例1-使用api¶
#!/usr/bin/env python
'''
This example demonstrates a simple use of pycallgraph.
'''
from pycallgraph import PyCallGraph
from pycallgraph.output import GraphvizOutput
class Banana:
def eat(self):
pass
class Person:
def __init__(self):
self.no_bananas()
def no_bananas(self):
self.bananas = []
def add_banana(self, banana):
self.bananas.append(banana)
def eat_bananas(self):
[banana.eat() for banana in self.bananas]
self.no_bananas()
def main():
graphviz = GraphvizOutput()
graphviz.output_file = 'basic.png'
with PyCallGraph(output=graphviz):
person = Person()
for a in xrange(10):
person.add_banana(Banana())
person.eat_bananas()
if __name__ == '__main__':
main()
执行:
python demo1.py
# 即生成如下图形
![https://img.zhaoweiguo.com/uPic/2024/06/O0ho9u.png](https://img.zhaoweiguo.com/uPic/2024/06/O0ho9u.png)
示例2-使用命令¶
#!/usr/bin/env python
class Banana:
def eat(self):
pass
class Person:
def __init__(self):
self.no_bananas()
def no_bananas(self):
self.bananas = []
def add_banana(self, banana):
self.bananas.append(banana)
def eat_bananas(self):
[banana.eat() for banana in self.bananas]
self.no_bananas()
def main():
person = Person()
for a in range(10):
person.add_banana(Banana())
person.eat_bananas()
if __name__ == '__main__':
main()
执行:
pycallgraph graphviz -- ./demo2.py