首页 技术 正文
技术 2022年11月10日
0 收藏 337 点赞 3,024 浏览 8800 个字

.ckrating_highly_rated {background-color:#FFFFCC !important;}
.ckrating_poorly_rated {opacity:0.6;filter:alpha(opacity=60) !important;}
.ckrating_hotly_debated {background-color:#FFF0F5 !important;}

.syntaxhighlighter,
.syntaxhighlighter a,
.syntaxhighlighter div,
.syntaxhighlighter code,
.syntaxhighlighter table,
.syntaxhighlighter table td,
.syntaxhighlighter table tr,
.syntaxhighlighter table tbody,
.syntaxhighlighter table thead,
.syntaxhighlighter table caption,
.syntaxhighlighter textarea {
font-size: 12px !important; /* Set the font size in pixels */
font-family: “Consolas”, “Bitstream Vera Sans Mono”, “Courier New”, Courier, monospace !important; /* Set the font type */
}
.syntaxhighlighter table caption {
/* For Title(Caption) */
font-size: 14px !important; /* Set the font size in pixels */
font-family: “Consolas”, “Bitstream Vera Sans Mono”, “Courier New”, Courier, monospace !important; /* Set the font type */
}
.syntaxhighlighter.nogutter td.code .line {
/* Set the left padding space when no-gutter in ver. 3.0 */
padding-left: 3px !important;
}
.syntaxhighlighter {
/* For Chrome/Safari(WebKit) */
/* Hide the superfluous vertical scrollbar in ver. 3.0 */
overflow-y: hidden !important;
padding: 1px !important;
}
.widget-area.syntaxhighlighter a,
.widget-area.syntaxhighlighter div,
.widget-area.syntaxhighlighter code,
.widget-area.syntaxhighlighter table,
.widget-area.syntaxhighlighter table td,
.widget-area.syntaxhighlighter table tr,
.widget-area.syntaxhighlighter table tbody,
.widget-area.syntaxhighlighter table thead,
.widget-area.syntaxhighlighter table caption,
.widget-area.syntaxhighlighter textarea {
/* For Widget */
font-size: 14px !important; /* Set the font size in pixels */
font-family: “Consolas”, “Bitstream Vera Sans Mono”, “Courier New”, Courier, monospace !important; /* Set the font type */
}
.widget-area table caption {
/* For Widget */
/* For Title(Caption) */
font-size: 10px !important; /* Set the font size in pixels */
font-family: “Consolas”, “Bitstream Vera Sans Mono”, “Courier New”, Courier, monospace !important; /* Set the font type */
}
div.container{
width:inherit
}

*::selection { background: #0099cc; }
*::-moz-selection { background: #0099cc; }
body { background: #ffffff }
a, .tabs ul.nav li a:hover, .tabs ul.nav li.active a, .dropcap, .toggle.hover .toggle-title, li.comment cite a:hover, h3.widget-title, .post-meta .meta-title:hover, .the-latest a:hover h4, .aw_socialcounter_widget li a:hover, .aw_tabbed_widget #tab-latest-comments a:hover { color: #0099cc; }
a:hover { color: #b30000; }
input:focus, textarea:focus { border-color: #0099cc; }
#searchsubmit, .highlight, .aw_tabbed_widget .tabs ul.nav li.active a, footer .aw_tabbed_widget .tabs ul.nav li.active a, #top .aw_tabbed_widget .tabs ul.nav li.active a, .aw_tabbed_widget .tabs ul.nav li a:hover, footer .aw_tabbed_widget .tabs ul.nav li a:hover, #top .aw_tabbed_widget .tabs ul.nav li a:hover, .aw_twitter_widget .twitter-icon, .testimonial-icon, #top-closed:hover, .flex-control-nav a:hover, .flex-control-nav a.flex-active { background-color: #0099cc; }
.submit { background-color: #0099cc; border-color: #007399; }
.submit:hover { background-color: #b30000; border-color: #860000; }
#searchsubmit:hover { background-color: #b30000; }
.toggle.hover .toggle-icon { border-top-color: #0099cc; }
.toggle.hover.active .toggle-icon { border-bottom-color: #0099cc; }
.flex-direction-nav li .flex-prev:hover { border-right-color: #0099cc; }
.flex-direction-nav li .flex-next:hover { border-left-color: #0099cc; }
@media only screen and (min-width: 768px) and (max-width: 959px) {
.aw_tabbed_widget .tabs ul.nav li a:hover, .tabs ul.nav li.active a { color: #0099cc; }
}
@media screen and (max-width: 767px) {
.tabs ul.nav li a:hover, .tabs ul.nav li.active a { color: #0099cc; }
}

我常用的 Python 调试工具

本文由 trace模块,可以打印运行时包含在其中的模块里所有执行到的语句。(就像制作一份项目报告)

python -mtrace –trace script.py

这会产生大量输出(执行到的每一行都会被打印出来,你可能想要用grep过滤那些你感兴趣的模块).
比如:

python -mtrace –trace script.py | egrep '^(mod1.py|mod2.py)'

调试器

以下是如今应该人尽皆知的一个基础介绍:

import pdb
pdb.set_trace() # 开启pdb提示

或者

try:
(一段抛出异常的代码)
except:
import pdb
pdb.pm() # 或者 pdb.post_mortem()

或者(输入 c 开始执行脚本)

python -mpdb script.py

在输入-计算-输出循环(注:REPL,READ-EVAL-PRINT-LOOP的缩写)环境下,可以有如下操作:

  • c or continue
  • q or quit
  • l or list, 显示当前步帧的源码
  • w or where,回溯调用过程
  • d or down, 后退一步帧(注:相当于回滚)
  • u or up, 前进一步帧
  • (回车), 重复上一条指令

其余的几乎全部指令(还有很少的其他一些命令除外),在当前步帧上当作python代码进行解析。

如果你觉得挑战性还不够的话,可以试下smiley,-它可以给你展示那些变量而且你能使用它来远程追踪程序。

更好的调试器

pdb的直接替代者:
ipdb(easy_install ipdb) – 类似ipython(有自动完成,显示颜色等)
pudb(easy_install pudb) – 基于curses(类似图形界面接口),特别适合浏览源代码

远程调试器

安装方式:

sudo apt-get install winpdb

用下面的方式取代以前的pdb.set_trace():

import rpdb2
rpdb2.start_embedded_debugger("secretpassword")

现在运行winpdb,文件-关联

不喜欢Winpdb?也可以直接包装PDB在TCP之上运行!

这样做:

import loggging                class Rdb(pdb.Pdb):
"""
This will run pdb as a ephemeral telnet service. Once you connect no one
else can connect. On construction this object will block execution till a
client has connected. Based on https://github.com/tamentis/rpdb I think ... To use this:: Rdb(4444).set_trace() Then run: telnet 127.0.0.1 4444
"""
def __init__(self, port=0):
self.old_stdout = sys.stdout
self.old_stdin = sys.stdin
self.listen_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.listen_socket.bind(('0.0.0.0', port))
if not port:
logging.critical("PDB remote session open on: %s", self.listen_socket.getsockname())
print >> sys.__stderr__, "PDB remote session open on:", self.listen_socket.getsockname()
sys.stderr.flush()
self.listen_socket.listen(1)
self.connected_socket, address = self.listen_socket.accept()
self.handle = self.connected_socket.makefile('rw')
pdb.Pdb.__init__(self, completekey='tab', stdin=self.handle, stdout=self.handle)
sys.stdout = sys.stdin = self.handle def do_continue(self, arg):
sys.stdout = self.old_stdout
sys.stdin = self.old_stdin
self.handle.close()
self.connected_socket.close()
self.listen_socket.close()
self.set_continue()
return 1 do_c = do_cont = do_continue def set_trace():
"""
Opens a remote PDB on first available port.
"""
rdb = Rdb()
rdb.set_trace()

只想要一个REPL环境?试试IPython如何?
如果你不需要一个完整齐全的调试器,那就只需要用一下的方式启动一个IPython即可:

import IPython
IPython.embed()

标准linux工具

我常常惊讶于它们竟然远未被充分利用。你能用这些工具解决很大范围内的问题:从性能问题(太多的系统调用,内存分配等等)到死锁,网络问题,磁盘问题等等。
其中最有用的是最直接的strace,只需要运行 sudo strace -p 12345 或者 strace -f 指令(-f 即同时追踪fork出来的子进程),这就行了。输出一般会非常大,所以你可能想要把它重定向到一个文件以便作更多的分析(只需要加上 &> 文件名)。

再就是ltrace,有点类似strace,不同的是,它输出的是库函数调用。参数大体相同。

还有lsof 用来指出你在ltrace/strace中看到的句柄数值的意义。比如:

lsof -p 12345

更好的跟踪

使用简单而可以做很多事情-人人都该装上htop!

sudo apt-get install htop
sudo htop

现在找到那些你想要的进程,再输入:

s - 代表系统调用过程(类似strace)
L - 代表库调用过程(类似ltrace)
l - 代表lsof

监控

没 有好的持续的服务器监控,但是如果你曾遇到一些很诡异的情况,诸如为什么一切都运行的那么慢,那些系统资源都干什么去了,。。。等这些问题,想弄明白却又 无处下手之际,不必动用iotop,iftop,htop,iostat,vmstat这些工具,就用dstat吧!它可以做之前我们提过的大部分工作可 以做的事情,而且也许可以做的更好!
它会用一种紧凑的,代码高亮的方式(不同于iostat,vmstat)向你持续展示数据,你还经常可以看到过去的数据(不同于iftop,iostop,htop)。

只需运行:

dstat --cpu --io --mem --net --load --fs --vm --disk-util --disk-tps --freespace --swap --top-io --top-bio-adv

很可能有一种更简短的方式来写上面这条命令,

这是一个相当复杂而又强大的工具,但是这里我只提到了一些基本的内容(安装以及基础的命令)

sudo apt-get install gdb python-dbg
zcat /usr/share/doc/python2.7/gdbinit.gz > ~/.gdbinit

用python2.7-dbg 运行程序:

sudo gdb -p 12345

现在使用:

bt - 堆栈跟踪(C 级别)
pystack - python 堆栈跟踪,不幸的是你需要有~/.gdbinit 并且使用python-dbg
c - 继续

发生段错误?用faulthandler !

python 3.3版本以后新增的一个很棒的功能,可以向后移植到python2.x版本。只需要运行下面的语句,你就可以大抵知道什么原因引起来段错误。

import faulthandler
faulthandler.enable()

内存泄露

嗯,这种情况下有很多的工具可以使用,其中有一些专门针对WSGI的程序比如Dozer,但是我最喜欢的当然是objgraph。使用简单方便,让人惊讶!
它没有集成WSGI或者其他,所以你需要自己去发现运行代码的方法,像下面这样:

import objgraph
objs = objgraph.by_type("Request")[:15]
objgraph.show_backrefs(objs, max_depth=20, highlight=lambda v: v in objs,
filename="/tmp/graph.png")
Graph written to /tmp/objgraph-zbdM4z.dot (107 nodes)
Image generated as /tmp/graph.png

你会得到像这样一张(注意:它非常大)。你也可以得到一张输出。

内存使用

有时你想少用些内存。更少的内存分配常常可以使程序执行的更快,更好,用户希望内存合适好用)
有许多可用的工具,但在我看来最好用的是pytracemalloc。与其他工具相比,它开销非常小(不需要依赖于严重影响速度的sys.settrace)而且输出非常详尽。但安装起来比较痛苦,你需要重新编译python,但有了apt,做起来也非常容易。

只需要运行这些命令然后去吃顿午餐或者干点别的:

apt-get source python2.7
cd python2.7-*
wget? https://github.com/wyplay/pytracemalloc/raw/master/python2.7_track_free_list.patch
patch -p1 < python2.7_track_free_list.patch
debuild -us -uc
cd ..
sudo dpkg -i python2.7-minimal_2.7*.deb python2.7-dev_*.deb

接着安装pytracemalloc (注意如果你在一个virtualenv虚拟环境下操作,你需要在重新安装python后再次重建 – 只需要运行 virtualenv myenv)

pip install pytracemalloc

现在像下面这样在代码里包装你的应用程序

import tracemalloc, time
tracemalloc.enable()
top = tracemalloc.DisplayTop(
5000, # log the top 5000 locations
file=open('/tmp/memory-profile-%s' % time.time(), "w")
)
top.show_lineno = True
try:
# code that needs to be traced
finally:
top.display()

输出会像这样:

2013-05-31 18:05:07: Top 5000 allocations per file and line
#1: .../site-packages/billiard/_connection.py:198: size=1288 KiB, count=70 (+0),
average=18 KiB
#2: .../site-packages/billiard/_connection.py:199: size=1288 KiB, count=70 (+0),
average=18 KiB
#3: .../python2.7/importlib/__init__.py:37: size=459 KiB, count=5958 (+0),
average=78 B
#4: .../site-packages/amqp/transport.py:232: size=217 KiB, count=6960 (+0),
average=32 B
#5: .../site-packages/amqp/transport.py:231: size=206 KiB, count=8798 (+0),
average=24 B
#6: .../site-packages/amqp/serialization.py:210: size=199 KiB, count=822 (+0),
average=248 B
#7: .../lib/python2.7/socket.py:224: size=179 KiB, count=5947 (+0), average=30
B
#8: .../celery/utils/term.py:89: size=172 KiB, count=1953 (+0), average=90 B
#9: .../site-packages/kombu/connection.py:281: size=153 KiB, count=2400 (+0),
average=65 B
#10: .../site-packages/amqp/serialization.py:462: size=147 KiB, count=4704
(+0), average=32 B

很美,不是吗?

补充:更多有关调试的内容见这里

原文链接: 高磊

我常用的 Python 调试工具 – 博客 – 伯乐在线

关注后台技术,渴望将技术做到极致
新浪微博@kaulie

查看高磊的更多文章 >>

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,297