TL;DR threading.Timer uses system time but the time changes while I'm using it, how can I get it to use system uptime?
I have a Python script that does a bunch of stuff one of which is set the system time. When this script starts up the time is wrong. This script also needs to have a global timeout of 30 seconds.
I have been using the following timeout class:
class Timeout(object):
def __init__(self, seconds=1, signum=signal.SIGUSR1, exception=TimeoutException):
self.exception = exception
self.pid = os.getpid()
self.signum = signum
self.timer = threading.Timer(seconds, self.exit_function)
def exit_function(self):
os.kill(self.pid, self.signum)
def handle_timeout(self, signum, frame):
raise self.exception()
def __enter__(self):
signal.signal(self.signum, self.handle_timeout)
self.timer.start()
def __exit__(self, type, value, traceback):
self.timer.cancel()
Which wraps my entire script:
with Timeout(seconds=30):
main()
occasionally the script fails really quickly or never gets killed after the 30 seconds. I believe this is because threading.Timer
uses the system time which gets changed while the script is running. Is there anyway I can get it to use system uptime?