Check out this question. The correct answer has a great explanation on how to properly complete threads: Is there a way to kill a thread in Python?
To stop the flow by keyboard interrupt (ctrl + c), you can catch the “KeyboardInterrupt” exception and clear it before exiting. Like this:
try: start_thread() except (KeyboardInterrupt, SystemExit): cleanup_stop_thread() sys.exit()
This way you can control what to do when the program suddenly terminates.
You can also use the built-in signal module, which allows you to configure signal handlers (in your particular case, a SIGINT signal): http://docs.python.org/library/signal.html
rogeriopvl Apr 01 '10 at 10:51
source share