python定時器 apscheduler copy即用
準備
先 pip install apscheduler
下面展示一些 內聯代碼片
。
my_apscheduler .py
import datetime
from apscheduler.schedulers.blocking import BlockingScheduler
#獲取當前時間
def get_now_time():
now_time = datetime.datetime.now()
return datetime.datetime.strftime(now_time,'%Y-%m-%d %H:%M:%S')
scheduler = BlockingScheduler() # 後臺運行
# 設置爲每日凌晨8到10點,15-17點,1-21分 指定秒,每秒 調用一次當前任務
#秒, 如果配置了區間,每秒執行一次。
@scheduler.scheduled_job("cron", day_of_week='*', hour='8-10,15-17', minute='1-21', second='00-20,20-35,40')
def execute_method_name():
print(get_now_time()+"》》我執行咯,我執行咯")
if __name__ == '__main__':
try:
scheduler.start()
except (KeyboardInterrupt, SystemExit):
scheduler.shutdown()
參考鏈接
鏈接: Python 定時任務框架 APScheduler 詳解
https://segmentfault.com/a/1190000011084828