解决方案

  1. pycallgraph(感觉直接用pycallgraph grahviz命令生成的图并不是我想要的)
  2. 如何去阅读并学习一些优秀的开源框架的源码? - mailto1587的回答 - 知乎

    https://www.zhihu.com/question/26766601/answer/33952627
  3. traceback(没有图,直接在控制台打印)

解决方案2(用django来示范)

# django里的一个view
# views.py class ArticleSearchView(View): def get(self, request):
form = SearchForm(request.GET)
from .utils import cheese # 在这里插入
cheese() # 在这里插入
if form.is_valid():
keyword = form.cleaned_data.get("keyword")
if keyword:
article_list = Article.objects.filter(title__icontains=keyword)
return render(request, 'blog/search.html', {'form': form, 'article_list': article_list})
else:
form = SearchForm()
return render(request, 'blog/search.html', {'form': form, 'article_list': False, })

# utils.py
from __future__ import unicode_literals def cheese(frame=None, slient=False):
import sys
import tempfile
import webbrowser
import pygraphviz as pgv if not frame:
frame = sys._getframe().f_back G = pgv.AGraph(strict=False, directed=True) stack = [] node_set = set()
subgraph_set = {} while frame:
filename = frame.f_code.co_filename
firstlineno = frame.f_code.co_firstlineno
function = frame.f_code.co_name node = '{0}:{1}:{2}'.format(filename, firstlineno, function)
if node not in node_set:
node_set.add(node)
if filename not in subgraph_set:
subgraph_set[filename] = G.add_subgraph(
name='cluster' + filename,
label=filename
)
subgraph = subgraph_set[filename]
subgraph.add_node(
node,
label='{0}:{1}'.format(firstlineno, function)
) stack.append(frame)
frame = frame.f_back stack.reverse() len_stack = len(stack) for index, start in enumerate(stack): if index + 1 < len_stack:
start_filename = start.f_code.co_filename
start_firstlineno = start.f_code.co_firstlineno
start_function = start.f_code.co_name
start_lineno = start.f_lineno
start_subgraph = subgraph_set[start_filename] end = stack[index + 1]
end_filename = end.f_code.co_filename
end_firstlineno = end.f_code.co_firstlineno
end_function = end.f_code.co_name
end_subgraph = subgraph_set[end_filename] if index == 0:
color = 'green'
elif index == len_stack - 2:
color = 'red'
else:
color = 'black' G.add_edge(
'{0}:{1}:{2}'.format(start_filename,
start_firstlineno,
start_function),
'{0}:{1}:{2}'.format(end_filename,
end_firstlineno,
end_function),
color=color,
ltail=start_subgraph.name,
lhead=end_subgraph.name,
label='#{0} at {1}'.format(index + 1, start_lineno)
) fd, name = tempfile.mkstemp('.png') G.draw(name, prog='dot')
G.close() if not slient:
webbrowser.open('file://' + name) return name

解决方案2的结果图

解决方案3(用Flask来示范)

from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
import traceback
traceback.print_stack()
return 'Yet another hello!' if __name__ == '__main__':
app.run()

解决方案3的结果图

Python - 运行流程图, call graph, 调用图的更多相关文章

  1. 使用gprof2dot和graphivz生成程序运行调用图

    使用gprof2dot和graphivz生成程序运行调用图 gprof2dot是一个将gprof生成的输出转换为dot脚本的工具.通过给定一个gprof的输出文件,将其转换为生成程序调用图的dot脚本 ...

  2. CodeViz产生函数调用图

    一.安装过程   1.CodeViz的安装 0).确认你已经安装Graphviz[ http://www.graphviz.org/] 1).下载CodeViz.[ http://www.csn.ul ...

  3. 如何在本地使用scala或python运行Spark程序

    如何在本地使用scala或python运行Spark程序   包含两个部分: 本地scala语言编写程序,并编译打包成jar,在本地运行. 本地使用python语言编写程序,直接调用spark的接口, ...

  4. Python黑帽编程1.3 Python运行时与包管理工具

    Python黑帽编程1.3  Python运行时与包管理工具 0.1  本系列教程说明 本系列教程,采用的大纲母本为<Understanding Network Hacks Attack and ...

  5. Java和Python运行速度对比

    Java和Python运行速度对比:同一个函数运行一百万次,Java耗时0.577秒,Python耗时78秒--135倍的差距. 版本:Java 8,Python 2.7.10 Java测试代码: i ...

  6. python 运行python manege.py runserver时报错:“no module named djangorestframework” 的解决方案

    python 运行python manege.py runserver时报错:“no module named djangorestframework” 的解决方案 importerror:no mo ...

  7. Python运行Google App Engineer时出现的UnicodeDecodeError错误解决方案

    #Python运行Google App Engineer时出现的UnicodeDecodeError错误解决方案   ##问题描述 使用Python2.7.x运行GAE时有时会报这个错误 ```py ...

  8. 嵌入式Linux系统运行流程图

    /************************************************************************ * 嵌入式Linux系统运行流程图 * 说明: * ...

  9. calltree看代码调用图

    calltree是在linux下面看c代码(尤其是复杂的内核代码)的神器. 推荐  calltree+vim + ctags + cscope + taglist [ vim: 搭建vim看代码的环境 ...

随机推荐

  1. orm 相关

    优化

  2. AcWing 275. 传纸条

    #include<iostream> using namespace std ; ; *N][N][N]; int w[N][N]; int n,m; int main() { cin&g ...

  3. 题解 【Codefoeces687B】Remainders Game

    题意: 给出c1,c2,...cn,问对于任何一个正整数x,给出x%c1,x%c2,...的值x%k的值是否确定; 思路: 中国剩余定理.详见https://blog.csdn.net/acdream ...

  4. C++——绪论

    计算机语言的发展 1.机器语言(二进制).汇编语言.比较难以理解和识记,与人类语言之间的差距太大: 2.高级语言,可以写出类似于人类思维的语句,可以有人们习惯的表达方式: 3.面向对象的语言,描述客观 ...

  5. MyEclipse 安装 emmet 插件

    1.在线安装 地址:http://download.emmet.io/eclipse/updates/ 安装完成后重新启动myeclipse 2.离线安装 下载jar包:https://github. ...

  6. 并查集-G - 食物链

    G - 食物链 动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A.现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我们并不知道它到底是哪一种 ...

  7. 2D TOOLKIT备忘录

    通过Clip名称更改动画片段 // 通过clip名称获取clip实例 tk2dSpriteAnimationClip clip = childrenAnimator.GetClipByName(&qu ...

  8. mysql之case..when ..then..else..end as..用法

    1.示例1 查询1: SELECT CASE main_xm_sam31 WHEN '02' THEN 2 ELSE 1 END AS SPDJ FROM SR_MAIN_BG A WHERE A.P ...

  9. util之Set

    1.定义: Set<Integer>set = new TreeSet<Integer>(); 注意: TreeSet 是二差树实现的,Treeset中的数据是自动排好序的,不 ...

  10. TCP/IP详解,卷1:协议--ARP:地址解析协议

    引言 本章我们要讨论的问题是只对 T C P / I P 协议簇有意义的 I P 地址.数据链路如以太网或令牌 环网都有自己的寻址机制(常常为 48 bit 地址),这是使用数据链路的任何网络层都必须 ...