Python thread dumps.

2008-12-17

For some time I’ve wanted the equivalent of Java’s ability to dump the stack trace of all currently running threads in Python as a means for debugging some hung processes. I finally found a solution and wired it up to the services’ http console:

import sys
import traceback
from pygments import highlight
from pygments.lexers import PythonLexer
from pygments.formatters import HtmlFormatter
 
def stacktraces():
    code = []
    for threadId, stack in sys._current_frames().items():
        code.append("\n# ThreadID: %s" % 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()))
 
    return highlight("\n".join(code), PythonLexer(), HtmlFormatter(
      full=False,
      # style="native",
      noclasses=True,
    ))

The magic happens with sys._current_frames() which returns exactly what I wanted. The only outstanding issue is how to get the thread’s name to display in addition to the ident.

I’ll probably hook this up as a signal handler as well so headless applications can have the same functionality.

5 comments

  1. This is super sexy swinging code.

    ntresch, December 17, 2008
  2. thanks,this is what I want,I google it a lot of times.
    just like you ,I used to use stacktrace in java to peek what is going wrong with thread, so I want to do it in Python,and finally, I get you answer.
    thanks!
    this article save me a lot of time and energy.

    dape, August 17, 2009
  3. Great! I hope it’s working for you. I’ve found it to be valuable on more than one occasion.

    bzimmer, August 25, 2009
  4. I just want to +1 the praise for this. It is so neat (in the old sense of the word :-) ). I have just added it to a signal.SIGQUIT handler in my server, so I have java like kill -3 functionality.

    rtmie, September 30, 2009
  5. @rtmie, Thanks!

    bzimmer, October 1, 2009

Leave a comment