从运行的Python应用程序显示堆栈跟踪 [英] Showing the stack trace from a running Python application

查看:165
本文介绍了从运行的Python应用程序显示堆栈跟踪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



有没有什么办法来指示Python解释器来显示你确切的代码正在运行?



某种即时堆栈跟踪?



相关问题: / em>




解决方案

我有这样的模块 - 一个进程将长时间运行,但由于未知和不可复制的原因而被卡住。它有点黑客,只适用于unix(需要信号):

  import code,traceback,signal 

def debug(sig,frame):
中断运行进程,并为
交互式调试提供了一个python提示。
d = {'_ frame':frame} #允许访问帧对象。
d.update(frame.f_globals)#除非被全局
d.update(frame.f_locals)

i = code.InteractiveConsole(d)
message = signal received:进入python shell.\\\
Traceback:\\\

message + =''.join(traceback.format_stack(frame))
i.interact(message)

def listen():
signal.signal(signal.SIGUSR1,debug)#注册处理程序

要使用,只需在程序启动时的某个时刻调用listen()函数(甚至可以将它放在site.py中,让所有的python程序都使用它),并让它运行。在任何时候,发送进程一个SIGUSR1信号,使用kill,或在python中:

  os.kill(pid,signal。 SIGUSR1)

这将导致程序在目前的位置中断到python控制台,显示堆栈跟踪,并让您操纵变量。使用control-d(EOF)继续运行(尽管请注意,您可能会在发出信号的时候中断任何I / O等,所以它不是完全非侵入性的。


$ b $我有另一个脚本做同样的事情,除了它通过管道与运行的进程进行通信(允许调试后台进程等),这里有一点很大的可以发布在这里 python cookbook配方


I have this Python application that gets stuck from time to time and I can't find out where.

Is there any way to signal Python interpreter to show you the exact code that's running?

Some kind of on-the-fly stacktrace?

Related questions:

解决方案

I have module I use for situations like this - where a process will be running for a long time but gets stuck sometimes for unknown and irreproducible reasons. Its a bit hacky, and only works on unix (requires signals):

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

To use, just call the listen() function at some point when your program starts up (You could even stick it in site.py to have all python programs use it), and let it run. At any point, send the process a SIGUSR1 signal, using kill, or in python:

    os.kill(pid, signal.SIGUSR1)

This will cause the program to break to a python console at the point it is currently at, showing you the stack trace, and letting you manipulate the variables. Use control-d (EOF) to continue running (though note that you will probably interrupt any I/O etc at the point you signal, so it isn't fully non-intrusive.

I've another script that does the same thing, except it communicates with the running process through a pipe (to allow for debugging backgrounded processes etc). Its a bit large to post here, but I've added it as a python cookbook recipe.

这篇关于从运行的Python应用程序显示堆栈跟踪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆