How can I tell where my python script is hanging?

后端 未结 13 797
醉话见心
醉话见心 2020-12-04 08:10

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

13条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-04 08:34

    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 to except 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).

    exception KeyboardInterrupt

    Raised when the user hits the interrupt key (normally Control-C or Delete). 
    

    During execution, a check for interrupts is made regularly. Interrupts typed when a built-in function input() or raw_input() is waiting for input also raise this exception. The exception inherits from BaseException so as to not be accidentally caught by code that catches Exception and thus prevent the interpreter from exiting.

    Changed in version 2.5: Changed to inherit from BaseException.
    

提交回复
热议问题