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 小时等较长时间,则脚本根本无法运行。时间一到,什么都不会发生。您能否指出错误发生的原因并指导我修复它?提前致谢。