分析运行中的 Python 进程详细解析


Posted in Python onJune 22, 2019

在 Java 中打印当前线程的方法栈,可以用 kill -3 命令向 JVM 发送一个 OS 信号,JVM 捕捉以后会自动 dump 出来;当然,也可以直接使用 jstack 工具完成,这些方法好几年前我在这篇性能分析的文章 中介绍过。这样的需求可以说很常见,比如定位死锁,定位一个不工作的线程到底卡在哪里,或者定位为什么 CPU 居高不下等等问题。

现在工作中我用的是 Python,需要线上问题定位的缘故,也有了类似的需求——想要知道当前的 Python 进程“在干什么”。但是没有了 JVM 的加持,原有的命令或者工具都不再适用。传统的 gdb 的 debug 大法在线上也不好操作。于是我寻找了一些别的方法,来帮助定位问题,我把它们记录在这里。

signal

在代码中,我们可以使用 signal 为进程预先注册一个信号接收器,在进程接收到特定信号的时候,可以打印方法栈:

import traceback, signal
class Debugger():
  def __init__(self, logger):
    self._logger = logger
  def log_stack_trace(self, sig, frame):
    d={'_frame':frame}
    d.update(frame.f_globals)
    d.update(frame.f_locals)
    messages = "Signal received. Stack trace:\n"
    messages += ''.join(traceback.format_stack(frame))
    self._logger.warn(messages)
  def listen(self):
    signal.signal(signal.SIGUSR1, self.log_stack_trace)

通过调用上面的 listen 方法(比如 new Debug(logger).listen()),就将一个可以接收 SIGUSR1 并打印方法栈的接收器注册到当前进程了。这里是打印方法栈,但是实际上可以做任何事,因为方法执行的当前,上下文已经跑到进程里面了。

那么怎么向进程发送信号呢?和 JVM 的方法类似,可以通过操作系统命令来发送:

kill -30 pid

这里的信号为什么是 30?这是因为 SIGUSR1 被当前操作系统定义成 30(请注意不同的操作系统这个映射表是可能不同的),这点可以通过 man signal 查看:

No Name Default Action Description
 SIGHUP terminate process terminal line hangup
 SIGINT terminate process interrupt program
 SIGQUIT create core image quit program
 SIGILL create core image illegal instruction
 SIGTRAP create core image trace trap
 SIGABRT create core image abort program (formerly SIGIOT)
 SIGEMT create core image emulate instruction executed
 SIGFPE create core image floating-point exception
 SIGKILL terminate process kill program
 SIGBUS create core image bus error
 SIGSEGV create core image segmentation violation
 SIGSYS create core image non-existent system call invoked
 SIGPIPE terminate process write on a pipe with no reader
 SIGALRM terminate process real-time timer expired
 SIGTERM terminate process software termination signal
 SIGURG discard signal urgent condition present on socket
 SIGSTOP stop process stop (cannot be caught or ignored)
 SIGTSTP stop process stop signal generated from keyboard
 SIGCONT discard signal continue after stop
 SIGCHLD discard signal child status has changed
 SIGTTIN stop process background read attempted from control terminal
 SIGTTOU stop process background write attempted to control terminal
 SIGIO discard signal I/O is possible on a descriptor (see fcntl(2))
 SIGXCPU terminate process cpu time limit exceeded (see setrlimit(2))
 SIGXFSZ terminate process file size limit exceeded (see setrlimit(2))
 SIGVTALRM terminate process virtual time alarm (see setitimer(2))
 SIGPROF terminate process profiling timer alarm (see setitimer(2))
 SIGWINCH discard signal Window size change
 SIGINFO discard signal status request from keyboard
 SIGUSR1 terminate process User defined signal 1
 SIGUSR2 terminate process User defined signal 2

当然,也可以写一点点 python 脚本来发送这个信号:

import os, signal
os.kill($PID, signal.SIGUSR1)

原理是一样的。

strace

如果进程已经无响应了,或者上面的信号接收器没有注册,那么就要考虑别的方法来或者“进程在干什么”这件事情了。其中,一个有用的命令是 strace:

strace -p pid

比如,我自己写了一个测试脚本 t.py,使用 python 执行,然后调用 sleep,再给它发送一个 SIGUSR1 的消息,它打印方法栈并退出。这整个过程,我使用 strace 可以得到这样的结果:

strace -p 9157
strace: Process 9157 attached
select(0, NULL, NULL, NULL, {9999943, 62231}) = ? ERESTARTNOHAND (To be restarted if no handler)
--- SIGUSR1 {si_signo=SIGUSR1, si_code=SI_USER, si_pid=9273, si_uid=9007} ---
rt_sigreturn({mask=[]})         = -1 EINTR (Interrupted system call)
stat("t.py", {st_mode=S_IFREG|0644, st_size=1281, ...}) = 0
open("t.py", O_RDONLY)         = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=1281, ...}) = 0
fstat(3, {st_mode=S_IFREG|0644, st_size=1281, ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f631e866000
read(3, "import traceback, signal, time\n "..., 8192) = 1281
read(3, "", 4096)            = 0
close(3)                = 0
munmap(0x7f631e866000, 4096)      = 0
stat("t.py", {st_mode=S_IFREG|0644, st_size=1281, ...}) = 0
write(1, "Signal received. Stack trace:\n "..., 134) = 134
write(1, "\n", 1)            = 1
rt_sigaction(SIGINT, {SIG_DFL, [], SA_RESTORER, 0x7f631e06f5d0}, {0x7f631e392680, [], SA_RESTORER, 0x7f631e06f5d0}, 8) = 0
rt_sigaction(SIGUSR1, {SIG_DFL, [], SA_RESTORER, 0x7f631e06f5d0}, {0x7f631e392680, [], SA_RESTORER, 0x7f631e06f5d0}, 8) = 0
exit_group(0)              = ?
+++ exited with 0 +++

可以看到从 strace attached 开始,到进程退出,所有重要的调用都被打印出来了。

在 iOS 下,没有 strace,但是可以使用类似的(更好的)命令 dtruss。

lsof

lsof 可以打印某进程打开的文件,而 Linux 下面一切都是文件,因此查看打开的文件列表有时可以获取很多额外的信息。比如,打开前面提到的这个测试进程:

lsof -p 16872
COMMAND  PID USER  FD  TYPE DEVICE  SIZE/OFF   NODE NAME
Python 16872 xxx cwd  DIR  1,5    2688 1113586 /Users/xxx
Python 16872 xxx txt  REG  1,5   51744 10627527 /System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python
Python 16872 xxx txt  REG  1,5   52768 10631046 /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/_locale.so
Python 16872 xxx txt  REG  1,5   65952 10631134 /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/time.so
Python 16872 xxx txt  REG  1,5   841440 10690598 /usr/lib/dyld
Python 16872 xxx txt  REG  1,5 1170079744 10705794 /private/var/db/dyld/dyld_shared_cache_x86_64h
Python 16872 xxx  0u  CHR  16,2  0t39990   649 /dev/ttys002
Python 16872 xxx  1u  CHR  16,2  0t39990   649 /dev/ttys002
Python 16872 xxx  2u  CHR  16,2  0t39990   649 /dev/ttys002

它有几个参数很常用,比如-i,用来指定网络文件(如果是“-i: 端口号”这样的形式还可以指定端口)。

总结

以上所述是小编给大家介绍的分析运行中的 Python 进程,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

Python 相关文章推荐
python操作摄像头截图实现远程监控的例子
Mar 25 Python
Python实现从url中提取域名的几种方法
Sep 26 Python
python网络编程之文件下载实例分析
May 20 Python
python使用两种发邮件的方式smtp和outlook示例
Jun 02 Python
Python with语句上下文管理器两种实现方法分析
Feb 09 Python
numpy中的ndarray方法和属性详解
May 27 Python
Python爬虫动态ip代理防止被封的方法
Jul 07 Python
Python实现变声器功能(萝莉音御姐音)
Dec 05 Python
Python3搭建http服务器的实现代码
Feb 11 Python
Python unittest单元测试框架实现参数化
Apr 29 Python
keras 如何保存最佳的训练模型
May 25 Python
python字典按照value排序方法
Dec 28 Python
机器学习实战之knn算法pandas
Jun 22 #Python
解决py2exe打包后,总是多显示一个DOS黑色窗口的问题
Jun 21 #Python
pyinstaller打包单个exe后无法执行错误的解决方法
Jun 21 #Python
pyinstaller打包多个py文件和去除cmd黑框的方法
Jun 21 #Python
解决Pyinstaller 打包exe文件 取消dos窗口(黑框框)的问题
Jun 21 #Python
十行代码使用Python写一个USB病毒
Jun 21 #Python
Python pandas DataFrame操作的实现代码
Jun 21 #Python
You might like
php+jQuery.uploadify实现文件上传教程
2014/12/26 PHP
php使用curl_init()和curl_multi_init()多线程的速度比较详解
2018/08/15 PHP
php+ajax 文件上传代码实例
2019/03/18 PHP
File文件控件,选中文件(图片,flash,视频)即立即预览显示
2009/04/09 Javascript
js 加载并解析XML字符串的代码
2009/12/13 Javascript
javascript中window.open在原来的窗口中打开新的窗口(不同名)
2015/11/15 Javascript
完美实现八种js焦点轮播图(下篇)
2020/04/20 Javascript
详解http访问解析流程原理
2017/10/18 Javascript
vue2.x+webpack快速搭建前端项目框架详解
2017/11/30 Javascript
ajax请求+vue.js渲染+页面加载的示例
2018/02/11 Javascript
vue路由懒加载的实现方法
2018/03/12 Javascript
快速解决vue-cli不能初始化webpack模板的问题
2018/03/20 Javascript
Vue 父子组件数据传递的四种方式( inheritAttrs + $attrs + $listeners)
2018/05/04 Javascript
vue实现的下拉框功能示例
2019/01/29 Javascript
微信小程序实现的自定义分享功能示例
2019/02/12 Javascript
[02:33]DOTA2英雄基础教程 司夜刺客
2013/12/04 DOTA
[01:10:02]IG vs Winstrike 2018国际邀请赛小组赛BO2 第一场 8.19
2018/08/21 DOTA
设计模式中的原型模式在Python程序中的应用示例
2016/03/02 Python
python 字典(dict)按键和值排序
2016/06/28 Python
Python列表list操作符实例分析【标准类型操作符、切片、连接字符、列表解析、重复操作等】
2017/07/24 Python
pycharm安装和首次使用教程
2018/08/27 Python
python中将zip压缩包转为gz.tar的方法
2018/10/18 Python
新年快乐! python实现绚烂的烟花绽放效果
2019/01/30 Python
介绍一款python类型检查工具pyright(推荐)
2019/07/03 Python
python梯度下降算法的实现
2020/02/24 Python
python字典与json转换的方法总结
2020/12/28 Python
东南亚排名第一的服务市场:kaodim
2019/03/28 全球购物
weblogic面试题
2016/03/07 面试题
为什么在使用动态 SQL 语句时必须为低层数据库对象授予权限
2012/12/13 面试题
八年级历史教学反思
2014/01/10 职场文书
汉语言文学职业规划
2014/02/14 职场文书
JS新手入门数组处理的实用方法汇总
2021/04/07 Javascript
HTML页面滚动时部分内容位置固定不滚动的实现
2021/04/14 HTML / CSS
Pytorch 统计模型参数量的操作 param.numel()
2021/05/13 Python
详解使用 CSS prefers-* 规范提升网站的可访问性与健壮性
2021/05/25 HTML / CSS
IIS服务器中设置HTTP重定向访问HTTPS
2022/04/29 Servers