简体   繁体   中英

Python Script does not terminate properly

I have a python script which I call using python main.py on my terminal. It starts a Qt-GUI which executes properly and terminates when I close the GUI.

However, sometimes the last debug message "over and out" is printed but the script itself does not terminate. Neither ctrl + c , ctrl + d nor ctrl + z have any impact on the execution. It seems to me as if this happens when an exception was thrown inside the program (and caught by the GUI).

I do not know how to debug this since it obviously does not happen in the GUI itself. How do I debug this and find out, what I did wrong?

if __name__ == '__main__':
    import sys 

    app = QApplication(sys.argv)
    form = MainGui()
    form.show()
    app.exec_()
    print "over and out"

EDIT: It seems to me as if there is some thread still being active in the end. However, I do not explicetely work with thread (I do not know what Qt does internally...). Is there a way to view all running threads in the end?

EDIT2: Oh my f*ing god. The solution simply was restarting my system. Somehow my OS did some crazy stuff and prevented the script from terminating.

"Neither ctrl + c , ctrl + d nor ctrl + z have any impact on the execution."

Add these lines of code to the header of your program and ctrl + c will exit it.

import signal
signal.signal(signal.SIGINT, signal.SIG_DFL)

And if you want to automatically go into the pdb debugger when your program hits an exception, just do this:

import sys

def excepthook(type_, value, tb):
    import pdb
    import traceback

    # print the exception...
    traceback.print_exception(type_, value, tb)
    print
    # ...then start the debugger in post-mortem mode
    pdb.pm()

# we are NOT in interactive mode
if not hasattr(sys, 'ps1') or sys.stderr.target.isatty():
    # this stops PyQt from freezing the terminal
    from PyQt4.QtCore import pyqtRemoveInputHook
    pyqtRemoveInputHook()

    sys.excepthook = excepthook

The solution simply was restarting my system. Somehow my OS did some crazy stuff and prevented the script from terminating.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM