如何从Python Timer对象计算剩余时间。
timer = Timer(10, print, ("expiry"))
timer.start()
...
从这里开始,如何找出计时器到期前的剩余时间?
答案 0 :(得分:5)
在启动线程Timer
时设置一个值timer = Timer(10, print, ("expiry"))
....
start_time = time.time()
timer.start()
time.sleep(3)
....
print ("Running for : %s seconds"%(time.time()-start_time))
您可以随时制作自己的计时器课程
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())