Date Published:
Last Modified:

Entering A Running Python Process

pyrasite-shell is a great tool for attaching to a running Python process. You can install it using pip with:

$ pip install pyrasite

You can then attach a pyrasite shell to a running python process using the process ID (PID):

$ pyrasite-shell 12345

You should then be presented with an interactive Python shell, and you can do things like print the stack trace for each thread:

import sys, traceback
for thread_id, frame in sys._current_frames().items():
    print(f'**** STACK TRACE FOR THREAD {thread_id} *****'
    print('***** END OF STACK TRACE *****')

Note that annoyingly, coping and pasting this code into the interactive console will not usually work (due to whitespace getting mangled perhaps?).

pyrasite-shell is a great tool for attaching to “hung” or “frozen” Python processes, which might be stuck in a loop or stuck in a blocked state waiting for something to happen.

Like this page? Upvote with shurikens!


    comments powered by Disqus