Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force python's VM to print a stack trace?

I'm dealing with a python-written server that locks up, and stops working, including logging. I wonder if there's a python equivalent to java's "kill -3" signal that at least prints the current stacktrace.

like image 976
spike Avatar asked Jan 29 '10 21:01

spike


People also ask

How do I print a stack trace in Python?

Method 1: By using print_exc() method. This method prints exception information and stack trace entries from traceback object tb to file.

How do I get stack trace?

We can obtain a stack trace from a thread by calling the getStackTrace() method on the Thread instance. It returns an array of StackTraceElement, from which details about stack frames of the thread can be found.


2 Answers

Use the faulthandler module. https://pypi.python.org/pypi/faulthandler/

import faulthandler
faulthandler.register(signal.SIGUSR1)

This works outside of Python's interpreter loop's signal handling at the C level so it will work even when the Python interpreter itself is hung waiting on something else.

See also: http://docs.python.org/dev/library/faulthandler

like image 158
gps Avatar answered Oct 07 '22 20:10

gps


import signal, traceback
def quit_handler(signum,frame):
    traceback.print_stack()
signal.signal(signal.SIGQUIT,quit_handler)
like image 41
John La Rooy Avatar answered Oct 07 '22 19:10

John La Rooy