jeffpkamp
jeffpkamp

Reputation: 2866

getting stack trace from separate thread in python

I'm writing a python program what uses Tkinter for windows and is running on several threads. Occasionally it will freeze and when I stop it, I don't get a stack trace. I tried getting the stack by running a separate thread with traceback.print_stack(), but this only prints the stack for that thread.

is there a way for me to get the stack trace from all threads, or will I need to check them each individually?

Is there a good way to continually monitor the stack so that i can figure out why my program becomes unresponsive?

Upvotes: 3

Views: 2789

Answers (1)

jeffpkamp
jeffpkamp

Reputation: 2866

SO the best thing I have found to do this is to put all the threads in a try loop, then when an exception occurs use traceback to print out the error. I also try to give my threads name so that they will tell me who they are when they experience a problem. An example is below.

import traceback
class mt_thread:
    def __init__(self,name):
        self.name=name
        thread.start_new_thread(self.thread_code,())

    def thread_code(self):
        try:
            "CODE HERE"
        except:
            print self.name,"ERROR"
            traceback.print_exc()

new_thread=my_thread("thread_1")

Upvotes: 1

Related Questions