我有这个非阻塞计时器:
import time
class Timer:
def __init__(self):
self.status = 0
def update(self, data, tv):
self.target = time.time() + data
self.status = 1
def set_status(self, state):
self.status = state
def get_status(self, tv):
return self.status
def get_left(self, tv):
return int(self.target-time.time())
当我想要在主循环中使用计时器时,它非常有用,例如:
t = Timer()
while 1:
#If timer is not active.
if t.get_status() == 0:
# Do some checks.
# Start timer with 60 seconds.
t.update(60)
#If timer is active.
if t.get_status() == 1:
print "%s seconds left." % str(t.get_left())
if t.get_left() <= 0:
t.set_status(0)
# Timer is done do some stuff.
time.sleep(1)
我希望它能够在不执行线程的情况下处理多个计时器。