florianletsch
florianletsch

Reputation: 465

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.

Upvotes: 5

Views: 1865

Answers (2)

florianletsch
florianletsch

Reputation: 465

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

Upvotes: 0

John Doe
John Doe

Reputation: 3506

"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

Upvotes: 1

Related Questions