#-*-coding:utf-8 -*-
__author__ = 'Administrator'
import os,threading,time
curTime=time.strftime("%Y-%M-%D",time.localtime())#記錄當前時間
execF=False
ncount=0
def execTask():
#具體任務執行內容
print("execTask executed!")
def timerTask():
global execF
global curTime
global ncount
if execF is False:
execTask()#判斷任務是否執行過,沒有執行就執行
execF=True
else:#任務執行過,判斷時間是否新的一天。如果是就執行任務
desTime=time.strftime("%Y-%M-%D",time.localtime())
if desTime > curTime:
execF = False#任務執行執行置值為
curTime=desTime
ncount = ncount+1
timer = threading.Timer(5,timerTask)
timer.start()
print("定時器執行%d次"%(ncount))
if __name__=="__main__":
timer = threading.Timer(5,timerTask)
timer.start()
#######################################
from threading import Timer def hello(): print("hello, world") # 指定10秒后執行hello函數 t = Timer(10.0, hello) t.start()
##############################
from threading import Timer import time # 定義總共輸出幾次的計數器 count = 0 def print_time(): print("當前時間:%s" % time.ctime()) global t, count count += 1 # 如果count小於10,開始下一次調度 if count < 10: t = Timer(1, print_time) t.start() # 指定1秒后執行print_time函數 t = Timer(1, print_time) t.start()
##################################################
from apscheduler.schedulers.blocking import BlockingScheduler from datetime import datetime # 輸出時間 def job(): print(datetime.now().strtime("%Y-%m-%d %H:%M:%S")) # BlockingScheduler scheduler = BlockingScheduler() scheduler.add_job(job, "cron", day_of_week="1-5", hour=6, minute=30) #每周一到周五6:30執行任務 schduler.start()