# _*_ coding: utf-8 _*_ """ è°åº¦çä½¿ç¨ """ import time import datetime from threading import Timer from apscheduler.schedulers.blocking import BlockingScheduler from apscheduler.schedulers.background import BackgroundScheduler def print_hello(): print("TimeNow in func: %s" % (datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))) return if __name__ == "__main__": # 1. 使ç¨Threading模åä¸çTimer # t = Timer(2, print_hello) # # print("TimeNow start: %s" % (datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))) # t.start() # print("TimeNow end: %s" % (datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))) # exit() # 2. BlockingSchedulerï¼å¨è¿ç¨ä¸è¿è¡å个任å¡ï¼è°åº¦å¨æ¯å¯ä¸è¿è¡çä¸è¥¿, éç¨é»å¡çæ¹å¼ scheduler = BlockingScheduler() # éç¨åºå®æ¶é´é´éï¼intervalï¼çæ¹å¼ï¼æ¯é5ç§éæ§è¡ä¸æ¬¡ scheduler.add_job(print_hello, "interval", seconds=5) # éç¨dateçæ¹å¼ï¼å¨ç¹å®æ¶é´åªæ§è¡ä¸æ¬¡ # scheduler.add_job(print_hello, "date", run_date=datetime.datetime.now() + datetime.timedelta(seconds=5)) # print("TimeNow start: %s" % (datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))) # scheduler.start() # print("TimeNow end: %s" % (datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))) # exit() # 3. BackgroundScheduler: éåäºè¦æ±ä»»ä½å¨ç¨åºåå°è¿è¡çæ åµï¼å½å¸æè°åº¦å¨å¨åºç¨åå°æ§è¡æ¶ä½¿ç¨ãéç¨éé»å¡çæ¹å¼ scheduler = BackgroundScheduler() # éç¨åºå®æ¶é´é´éï¼intervalï¼çæ¹å¼ï¼æ¯é3ç§éæ§è¡ä¸æ¬¡ scheduler.add_job(print_hello, "interval", seconds=5) # è¿æ¯ä¸ä¸ªç¬ç«ççº¿ç¨ # print("TimeNow start: %s" % (datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))) # scheduler.start() # print("TimeNow end: %s" % (datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))) # while True: # time.sleep(2)