Getting the Stack Trace of a Running Python Program

I recently wanted to examine a multi-threaded Python program, which took a long time to complete, appeared to be stuck, and even crashed occasionally.Having a previous background in java, I was hoping to get the stack trace with some utility like jstac…


This content originally appeared on Level Up Coding - Medium and was authored by Peter Kogan

I recently wanted to examine a multi-threaded Python program, which took a long time to complete, appeared to be stuck, and even crashed occasionally.

Having a previous background in java, I was hoping to get the stack trace with some utility like jstack. To my surprise, Python doesn’t have such a utility, but it’s easy to make one ourselves!

The code

It’s pretty obvious — go over the running threads, format the stack trace and append it, then return it.

It uses pure Python libraries, no addons are required.

A sample output might look like this (focus mainly on the bottom part):

Testing

The output above is the result of the print statement in the test — note that we are expecting to see two threads: the main test thread and the one we just created and the test function and test statements in the stack trace.

Next Steps / Caveats

So we have the util function and it works, but we need to incorporate it in our code:

  1. We can have an entry point like a REST API if our program is a web server to execute it upon demand.
  2. Or, we can have a daemon thread running and occasionally printing the output so we have an ongoing understanding of what’s going on with our program.

That’s it! I hope that this small utility can help you out in times of need.


Getting the Stack Trace of a Running Python Program was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Peter Kogan


Print Share Comment Cite Upload Translate Updates
APA

Peter Kogan | Sciencx (2021-08-17T18:36:10+00:00) Getting the Stack Trace of a Running Python Program. Retrieved from https://www.scien.cx/2021/08/17/getting-the-stack-trace-of-a-running-python-program/

MLA
" » Getting the Stack Trace of a Running Python Program." Peter Kogan | Sciencx - Tuesday August 17, 2021, https://www.scien.cx/2021/08/17/getting-the-stack-trace-of-a-running-python-program/
HARVARD
Peter Kogan | Sciencx Tuesday August 17, 2021 » Getting the Stack Trace of a Running Python Program., viewed ,<https://www.scien.cx/2021/08/17/getting-the-stack-trace-of-a-running-python-program/>
VANCOUVER
Peter Kogan | Sciencx - » Getting the Stack Trace of a Running Python Program. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/08/17/getting-the-stack-trace-of-a-running-python-program/
CHICAGO
" » Getting the Stack Trace of a Running Python Program." Peter Kogan | Sciencx - Accessed . https://www.scien.cx/2021/08/17/getting-the-stack-trace-of-a-running-python-program/
IEEE
" » Getting the Stack Trace of a Running Python Program." Peter Kogan | Sciencx [Online]. Available: https://www.scien.cx/2021/08/17/getting-the-stack-trace-of-a-running-python-program/. [Accessed: ]
rf:citation
» Getting the Stack Trace of a Running Python Program | Peter Kogan | Sciencx | https://www.scien.cx/2021/08/17/getting-the-stack-trace-of-a-running-python-program/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.