Python Timer对支持的最长时间有一些限制吗?

时间:2013-01-18 14:17:03

标签: python timer

import threading
import os

def shutdown():
    os.system("shutdown -s")

# user setting zone!!!
hour = 0
minute = 20
sec = 0
# user setting zone!!!

total_sec = hour*3600.0 + minute*60.0 + sec - 60.0

if total_sec < 0:
    total_sec = 0
print("The computer will be Shut Down in (%d hour, %d minute, %d second).\n" %(hour, minute, sec))

if total_sec >= 120:
    temp_sec = total_sec - 120
    threading.Timer(temp_sec, lambda: print("Last 3 minutes before shuting down the computer!!\n")).start()
else:
    print("Less than 3 minutes before shuting down the computer!!\n")

threading.Timer(total_sec, shutdown).start()

代码如上所示。当我设置10分钟,20分钟或更长时间的短时间时,脚本可以正常工作。但是,如果我将等待时间设置为4小时或5小时的长时间,则脚本根本无法工作。时间到了,什么都不会发生。你能指出错误发生的原因并指导我修复它吗?提前谢谢。

1 个答案:

答案 0 :(得分:0)

你真的计时了吗?你说它工作正常,但实际上它设置为10分钟后会在10分钟内关闭电脑,而不是说15分钟以上吗?

我问,因为看起来你已经准备好给你一个3分钟的警告。然而,计时器重置是因为你在“threading.Timer(total_sec,shutdown).start()中使用total_sec”所以当你设置60分钟时,它会在57分钟时发出警告,然后再运行60分钟。

因此,我怀疑你是否让它运行了11个小时,当你设置它5个小时时,它实际上会关闭计算机。