如下所示:
''' Created on 2018-4-20 例子:每天凌晨3点执行func方法 ''' import datetime import threading def func(): print("haha") #如果需要循环调用,就要添加以下方法 timer = threading.Timer(86400, func) timer.start() # 获取现在时间 now_time = datetime.datetime.now() # 获取明天时间 next_time = now_time + datetime.timedelta(days=+1) next_year = next_time.date().year next_month = next_time.date().month next_day = next_time.date().day # 获取明天3点时间 next_time = datetime.datetime.strptime(str(next_year)+"-"+str(next_month)+"-"+str(next_day)+" 03:00:00", "%Y-%m-%d %H:%M:%S") # # 获取昨天时间 # last_time = now_time + datetime.timedelta(days=-1) # 获取距离明天3点时间,单位为秒 timer_start_time = (next_time - now_time).total_seconds() print(timer_start_time) # 54186.75975 #定时器,参数为(多少时间后执行,单位为秒,执行的方法) timer = threading.Timer(timer_start_time, func) timer.start()
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。