• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

python 搞代码 4年前 (2022-01-09) 19次浏览 已收录 0个评论

在 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 pro<i>本文来源gaodai$ma#com搞$$代**码)网@</i>cess 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

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:分析运行中的 Python 进程详细解析
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址