擅长:python、mysql、java
<p>您每次都在创建一个<code>Timer</code>实例。把它从循环中拿走,否则你的while循环永远不会终止。此外,还需要调用<code>timing.isExpired</code>,因为它是一个方法。所以你的代码应该是:</p>
<pre><code>import time
TIMEOUT = 60 * 5
class Timer ():
def __init__(self):
self.timeout = time.time()+TIMEOUT
def isExpired (self):
return time.time() > self.timeout
def main():
timing = Timer()
while 1:
if timing.isExpired():
print "Expired"
return
else:
print "Not expired"
print "sleeping for 1 second"
time.sleep(1)
</code></pre>