How do I calculate the remaining time from the Python Timer object.
timer = Timer(10, print, ("expiry"))
timer.start()
...
from here, how do I find out what is the remaining time before the timer expired?
just set a value when you start the thread Timer
timer = Timer(10, print, ("expiry"))
....
start_time = time.time()
timer.start()
time.sleep(3)
....
print ("Running for : %s seconds"%(time.time()-start_time))
You could always make your own timer class
class MyTimer(threading._Timer):
started_at = None
def start(self):
self.started_at = time.time()
threading._Timer.start(self)
def elapsed(self):
return time.time() - self.started_at
def remaining(self):
return self.interval - self.elapsed()
timer = MyTimer(10, print, ("expiry"))
timer.start()
for i in range(5):
time.sleep(1)
print (timer.remaining())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With