我有一个Python应用程序,它不时卡住,我不知道在哪里。
是否有任何方法向Python解释器发出信号,以显示正在运行的确切代码?
某种飞行中的堆叠痕迹?
相关问题:
从Python代码中的方法打印当前调用堆栈 检查正在运行的进程正在做什么:打印未检测的Python程序的堆栈跟踪
我有一个Python应用程序,它不时卡住,我不知道在哪里。
是否有任何方法向Python解释器发出信号,以显示正在运行的确切代码?
某种飞行中的堆叠痕迹?
相关问题:
从Python代码中的方法打印当前调用堆栈 检查正在运行的进程正在做什么:打印未检测的Python程序的堆栈跟踪
当前回答
我几乎总是处理多线程,主线程通常不做太多,所以最有趣的是转储所有堆栈(这更像Java的转储)。下面是一个基于这个博客的实现:
import threading, sys, traceback
def dumpstacks(signal, frame):
id2name = dict([(th.ident, th.name) for th in threading.enumerate()])
code = []
for threadId, stack in sys._current_frames().items():
code.append("\n# Thread: %s(%d)" % (id2name.get(threadId,""), threadId))
for filename, lineno, name, line in traceback.extract_stack(stack):
code.append('File: "%s", line %d, in %s' % (filename, lineno, name))
if line:
code.append(" %s" % (line.strip()))
print("\n".join(code))
import signal
signal.signal(signal.SIGQUIT, dumpstacks)
其他回答
我有一个用于这种情况的模块——一个进程将运行很长时间,但有时由于未知和不可复制的原因而卡住。它有点俗气,只在unix上工作(需要信号):
import code, traceback, signal
def debug(sig, frame):
"""Interrupt running process, and provide a python prompt for
interactive debugging."""
d={'_frame':frame} # Allow access to frame object.
d.update(frame.f_globals) # Unless shadowed by global
d.update(frame.f_locals)
i = code.InteractiveConsole(d)
message = "Signal received : entering python shell.\nTraceback:\n"
message += ''.join(traceback.format_stack(frame))
i.interact(message)
def listen():
signal.signal(signal.SIGUSR1, debug) # Register handler
要使用,只需在程序启动时调用listen()函数(您甚至可以将其插入site.py以让所有python程序使用它),并让它运行。在任何时候,使用kill或python向进程发送一个SIGUSR1信号:
os.kill(pid, signal.SIGUSR1)
这将导致程序在当前位置中断到python控制台,向您显示堆栈跟踪,并允许您操作变量。使用control-d (EOF)继续运行(不过请注意,您可能会在您发出信号时中断任何I/O等,因此它不是完全非侵入式的。
我有另一个脚本,做同样的事情,除了它通过管道与正在运行的进程通信(允许调试后台进程等)。在这里发布有点大,但我已经把它作为python食谱添加了。
如何调试任何函数在控制台:
创建使用pdb.set_trace()的函数,然后是要调试的函数。
>>> import pdb
>>> import my_function
>>> def f():
... pdb.set_trace()
... my_function()
...
然后调用创建的函数:
>>> f()
> <stdin>(3)f()
(Pdb) s
--Call--
> <stdin>(1)my_function()
(Pdb)
愉快的调试:)
我是GDB阵营的python扩展。关注https://wiki.python.org/moin/DebuggingWithGdb,这意味着
DNF install GDB python-debuginfo或sudo apt-get install GDB python2.7-dbg GDB python <运行进程>的pid py-bt
同时考虑info threads和thread apply all py-bt。
Python -dv yourscript.py
这将使解释器以调试模式运行,并为您提供解释器正在做什么的跟踪。
如果你想交互调试代码,你应该像这样运行它:
Python -m PDB yourscript.py
它告诉python解释器使用模块“pdb”(python调试器)运行你的脚本,如果你这样运行它,解释器将以交互模式执行,很像GDB
pyrasite可以在没有调试符号的情况下,在常规python中运行一个未准备好的python程序,从而获得该程序的堆栈跟踪。在Ubuntu Trusty上对我来说很有魅力:
$ sudo pip install pyrasite
$ echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope
$ sudo pyrasite 16262 dump_stacks.py # dumps stacks to stdout/stderr of the python program
(向@Albert致敬,在其他工具中,他的回答包含指向this的指针。)