Skip to content

Instantly share code, notes, and snippets.

@reywood
Last active September 18, 2023 01:35
Show Gist options
  • Star 39 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save reywood/e221c4061bbf2eccea885c9b2e4ef496 to your computer and use it in GitHub Desktop.
Save reywood/e221c4061bbf2eccea885c9b2e4ef496 to your computer and use it in GitHub Desktop.
How to get a stack trace from a stuck/hanging python script

How to get a stack trace for each thread in a running python script

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 and pyrasite

Install gdb.

# Redhat, CentOS, etc
$ yum install gdb

# Ubuntu, Debian, etc
$ apt-get update && apt-get install gdb

# MacOS
$ brew install gdb

Install pyrasite.

$ pip install pyrasite

Inspect process with 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.

from __future__ import print_function

import sys, traceback

for thread_id, frame in sys._current_frames().items():
    print('Stack for thread {}'.format(thread_id))
    traceback.print_stack(frame)
    print('')
@mariokostelac
Copy link

python 3 version

import sys, traceback

for thread_id, frame in sys._current_frames().items():
    print('Stack for thread {}'.format(thread_id))
    traceback.print_stack(frame)
    print('')

@soxofaan
Copy link

I've adapted this gist to Python 3 (and some additional tweaks): https://gist.github.com/soxofaan/58ae2e266b63a2471a1f1b49a133bac9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment