So I'm debugging my python program and have encountered a bug that makes the program hang, as if in an infinite loop. Now, I had a problem with an infinite loop before, but when it hung up I could kill the program and python spat out a helpful exception that told me where the program terminated when I sent it the kill command. Now, however, when the program hangs up and I ctrl-c it, it does not abort but continues running. Is there any tool I can use to locate the hang up? I'm new to profiling but from what I know a profiler can only provide you with information about a program that has successfully completed. Or can you use a profiler to debug such hang ups?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
If your program has more than one thread, it could be ignoring ctrl-c because the one thread is wired up to the ctrl-c handler, but the live (runaway?) thread is deaf to it. The GIL (global interpreter lock) in CPython means that normally only one thread can actually be running at any one time. I think I solved my (perhaps) similar problem using this
Haven't used it myself but I've heard that the Eric IDE is good and has a good debugger. That's also the only IDE I know of that has a debugger for Python
Wow ! Seems you added so much code in one go without testing it that you can't say what code was added just before program started to hang... (the most likely cause of problem).
Seriously, you should code by small steps and test each one individually (ideally doing TDD).
For your exact problem of spotting what python code is running and ctrl-c does not work, I will try a raw guess: did you used some
except:
catching all exceptions indistinctly. If you did so in a loop (and continue loop after managing exception), it's a very likely reason why ctrl-c does not work : it's catched by this exception. Change toexcept Exception:
and it should not be catched any more (there is other possibilities for ctrl+c not working like thread management as another poster suggested, but I believe the above reason is more likely).You could also try http://code.activestate.com/recipes/576515-debugging-a-running-python-process-by-interrupting/ . It should work as long as the Python process doesn't have signals masked, which is normally the case even if Ctrl-C doesn't work.
I wrote a module that prints out threads that hang longer that 10 seconds at one place. hanging_threads.py
Here is an example output:
This occurs at the exit of the main thread when you forget to set another thread as daemon.
It's easier to prevent these hang-ups than it is to debug them.
First:
for
loops are very, very hard to get stuck in a situation where the loop won't terminate. Very hard.Second:
while
loops are relatively easy to get stuck in a loop.The first pass is to check every
while
loop to see if it must be awhile
loop. Often you can replacewhile
constructs withfor
, and you'll correct your problem by rethinking your loop.If you cannot replace a
while
loop withfor
, then you simply have to prove that the expression in thewhile
statement must change every time through the loop. This isn't that hard to prove.Look at all the condition in the loop. Call this T.
Look at all the logic branches in the body of the loop. Is there any way to get through the loop without making a change to the condition, T?
Yes? That's your bug. That logic path is wrong.
No? Excellent, that loop must terminate.