Python定时器是否对支持的最长时间有一些限制?


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小时,脚本就根本无法工作。时间到了什么也不会发生。你能指出错误发生的原因并指导我修复吗?

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

我问是因为看起来你把它设置为给你3分钟的警告。然而,计时器重置,因为你使用total_sec在"线程"。计时器(total_sec关闭).start()"所以当你把它设置为60分钟时,它会在57分钟时给你一个警告,然后再运行60分钟。

因此,我怀疑如果你让它运行11个小时,当你设置为5小时时,它实际上会关闭电脑。

相关内容

最新更新