Dump stacktraces of all active Threads

后端 未结 6 1856
离开以前
离开以前 2020-12-06 04:24

I\'m trying to dump a list of all active threads including the current stack of each. I can get a list of all threads using threading.enumerate(), but i can\'t figure out a

6条回答
  •  自闭症患者
    2020-12-06 04:30

    As jitter points out in an earlier answer sys._current_frames() gives you what you need for v2.5+. For the lazy the following code snippet worked for me and may help you:

    print >> sys.stderr, "\n*** STACKTRACE - START ***\n"
    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()))
    
    for line in code:
        print >> sys.stderr, line
    print >> sys.stderr, "\n*** STACKTRACE - END ***\n"
    

提交回复
热议问题