How can I tell where my python script is hanging?

Solution 1:

Let's assume that you are running your program as:

python YOURSCRIPT.py

Try running your program as:

python -m trace --trace YOURSCRIPT.py

And have some patience while lots of stuff is printed on the screen. If you have an infinite loop, it will go on for-ever (halting problem). If it gets stuck somewhere, then mostly you are stuck on I/O or it is a deadlock.

Solution 2:

I wrote a module that prints out threads that hang longer that 10 seconds at one place. hanging_threads.py

Run:

python -m pip install hanging_threads

Add this to your code:

from hanging_threads import start_monitoring
start_monitoring(seconds_frozen=10, test_interval=100)

Here is an example output:

--------------------    Thread 5588     --------------------
  File "C:\python33\lib\threading.py", line 844, in _exitfunc
        t.join()
  File "C:\python33\lib\threading.py", line 743, in join
        self._block.wait()
  File "C:\python33\lib\threading.py", line 184, in wait
        waiter.acquire()

This occurs at the exit of the main thread when you forget to set another thread as daemon.

Solution 3:

Wow! 5 answers already and nobody has suggested the most obvious and simple:

  1. Try to find a reproducible test case that causes the hanging behavior.
  2. Add logging to your code. This can be as basic as print "**010", print "**020", etc. peppered through major areas.
  3. Run code. See where it hangs. Can't understand why? Add more logging. (I.e. if between **020 and **030, go and add **023, **025, **027, etc.)
  4. Goto 3.