How to do a sys.exit() using threading.Timer in python?
import threading import sys from time import time def stop(): print "stop" sys.exit() t = threading.Timer(10, stop) volts = 22 if volts > 20: t.start() print "Start"
After 10 seconds it prints Stop, which is fine, but it ignores the sys.exit(). I need to do a sys.exit when the timer expires.
When you run
inside of a thread, it raises the SystemExit exception. When you call thread.exit(), it raises the same exception, so you are only exiting your thread, not your program.
However in the case of the code you provided, exiting your thread will cause your program to finish as well.