Sometimes a python script will simply hang forever with no indication of where things went wrong. Perhaps it's polling a service that will never return a value that allows the program to move forward. Here's a way to see where the program is currently stuck.
Install gdb.
# Redhat, CentOS, etc
$ yum install gdb
# Ubuntu, Debian, etc
$ apt-get update && apt-get install gdb
Install pyrasite.
$ pip install pyrasite
Find the process ID for the stuck python process and run pyrasite-shell
with it.
# Assuming process ID is 12345
$ pyrasite-shell 12345
You should now see a python REPL. Run the following in the REPL to see stack traces for all threads.
import sys, traceback
for thread_id, frame in sys._current_frames().items():
print 'Stack for thread {}'.format(thread_id)
traceback.print_stack(frame)
print ''
and another script:
import sys, threading
def stacking():
print("\n*** STACKTRACE - START ***\n")
code = []
for threadId, stack in sys._current_frames().items():
threadName = ''
for t in threading.enumerate():
if t.ident == threadId:
threadName = t.name
code.append("\n# ThreadID: %s %s" % (threadId, threadName))
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(line)
print("\n*** STACKTRACE - END ***\n")