Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

debugging: how to check what where my Python program is hanging?

A fairly large Python program I write, runs, but sometimes, after running for minutes or hours, in a non easily reproducible moment, hangs and outputs nothing to the screen.

I have no idea what it is doing at that moment, and in what part of code it is.

How can I run this in a debugger or something to see what lines of code is the program executing in the moment it hangs?

Its too large to put "print" statements all over the place.

I did:

python -m trace --trace /usr/local/bin/my_program.py

but that gives me so much output that I can't really see anything, just millions of lines scrolling on the screen.

Best would be if I could send some signal to the program with "kill -SIGUSR1" or something, and at that moment the program would drop into a debugger and show me the line it stopped at and possibly allow me to step through the program then.

I've tried:

pdb usr/local/bin/my_program.py

and then:

(Pdb) cont

but what do I do to see where I am when it hangs? It doesn't throw and exception, just seems like it waits for something, possibly in an infinite loop.

One more detail: when the program hangs, and I press ^C and then (not sure if that is necessary) the program continues normally (without throwing any exception and without giving me any hint on the screen why did it stop).

like image 707
ria Avatar asked Feb 28 '11 18:02

ria


People also ask

How do I know which program is ending in Python?

Ctrl + C on Windows can be used to terminate Python scripts and Ctrl + Z on Unix will suspend (freeze) the execution of Python scripts. If you press CTRL + C while a script is running in the console, the script ends and raises an exception.

How do you trace a program in Python?

The trace module allows you to trace program execution, generate annotated statement coverage listings, print caller/callee relationships and list functions executed during a program run. It can be used in another program or from the command line.

How do I know if a Python script is stuck?

Inspect process with pyrasite Find the process ID for the stuck python process and run pyrasite-shell with it. You should now see a python REPL. Run the following in the REPL to see stack traces for all threads.


1 Answers

This could be useful to you. I usually do

>>> import pdb
>>> import program2debug
>>> pdb.run('program2debug.test()')

I usually add a -v option to my programs, which enables tons of print statements explaining what I'm doing in detail. When you write a program in the future, consider doing the same before it gets thousands of lines big.

like image 124
Patrick Avatar answered Sep 25 '22 15:09

Patrick