4.1 APS 定時框架

本節要點

  • 掌握 APS 定時框架;

課前準備

安裝Apscheduler

在cmd(Windows)或終端(Mac)輸入:

pip install apscheduler

課程內容

APS定時框架

APScheduler是基於Quartz的一個Python定時任務框架,實現了Quartz的所有功能,使用起來十分方便。提供了基於日期、固定時間間隔以及crontab類型的任務,並且可以持久化任務。基於這些功能,我們可以很方便的實現一個Python定時任務系統。

from apscheduler.schedulers.blocking import BlockingScheduler
scheduler = BlockingScheduler()
scheduler.add_job(<函數名>, <觸發器>, 觸發器的調度方法 , )
#scheduler.add_job(<函數名>, <觸發器>, args=(a,) , 觸發器的調度方法d , )
scheduler.start()

APS提供了三種觸發器供選擇,date, interval, cron可供選擇,其實看字面意思也可以知道,date表示具體的一次性任務,interval表示循環任務,cron表示定時任務

1. 一次性任務 - date

文件地址:點我查看

from datetime import datetime, timedelta
from apscheduler.schedulers.blocking import BlockingScheduler


def tick(x):
    print('Tick! The time is: %s' % datetime.now(), x)


if __name__ == '__main__':
    scheduler = BlockingScheduler(timezone='Asia/Shanghai')
    scheduler.add_job(tick, 'date', args=('一次性任務',),
                      run_date=datetime.now() + timedelta(seconds=12))
    scheduler.start()

run_date(datetime|str) – the date/time to run the job at

  • datetime.date(2009, 11, 6)
  • datetime。datetime(2009, 11, 6, 16, 30, 5)
  • ‘2019-11-06 16:30:05’

timezone(datetime.tzinfo|str) – time zone for run_date if it doesn’t have one already

  • 預設採用本地時區
  • 可以用pytz指定時區,timezone=pytz.utc

2. 間隔性任務 - intervals

文件地址:點我查看

from datetime import datetime
from apscheduler.schedulers.blocking import BlockingScheduler


def tick():
    print('Tick! The time is: %s' % datetime.now())


if __name__ == '__main__':
    scheduler = BlockingScheduler()
    scheduler.add_job(tick, 'interval', seconds=3)
    scheduler.start()

weeks(int) – number of weeks to wait

days(int) – number of days to wait

hours(int) – number of hours to wait

minutes(int) – number of minutes to wait

seconds(int) – number of seconds to wait

start_date(datetime|str) – starting point for the interval calculation

end_date(datetime|str) – latest possible date/time to trigger on

timezone(datetime.tzinfo|str) – time zone to use for the date/time calculations

3. 定時任務 - cron

文件地址:點我查看

import datetime
from apscheduler.schedulers.blocking import BlockingScheduler
def tick():
    print('Tick! The time is: %s' % datetime.now())
if __name__ == '__main__':
from datetime import datetime
from apscheduler.schedulers.blocking import BlockingScheduler
def tick():
    print('Tick! The time is: %s' % datetime.now())
if __name__ == '__main__':
    scheduler = BlockingScheduler()
    #每週週一到週日的下午17點20分10秒
    #scheduler.add_job(tick, 'cron', day_of_week='*' ,hour='17', minute='20', second='10')
    #每天的下午17點20分10秒
    #scheduler.add_job(tick, 'cron', hour='17', minute='20', second='10')
    #每年的第一週的下午17點20分10秒
    #scheduler.add_job(tick, 'cron', week='1' ,hour='17', minute='20', second='10')
    #每月的第1天的下午17點20分10秒
    #scheduler.add_job(tick, 'cron', day='1' ,hour='17', minute='20', second='10')
    #每月的第1天的下午17點20分0-59秒全部都會執行
    #scheduler.add_job(tick, 'cron', day='1' ,hour='17', minute='20', second='*')
    
    scheduler.start()

year(int|str) – 4-digit year

month(int|str) – month (1-12)

day(int|str) – day of the (1-31)

week(int|str) – ISO week (1-53)

day_of_week(int|str) – number or name of weekday (0-6 or mon,tue,wed,thu,fri,sat,sun)

hour(int|str) – hour (0-23)

minute(int|str) – minute (0-59)

second(int|str) – second (0-59)

start_date(datetime|str) – earliest possible date/time to trigger on (inclusive)

end_date(datetime|str) – latest possible date/time to trigger on (inclusive)

timezone(datetime.tzinfo|str) – time zone to use for the date/time calculations (defaults to scheduler timezone)

Expression

Field

Description

*

any

Fire on every value

*

/a

any

Fire every a values, starting from the minimum

a-b

any

Fire on any value within the a-b range (a must be smaller than b)

a-b/c

any

Fire every c values within the a-b range

xth y

day

Fire on the x -th occurrence of weekday y within the month

last x

day

Fire on the last occurrence of weekday x within the month

last

day

Fire on the last day within the month

x,y,z

any

Fire on any matching expression; can combine any number of any of the above expressions

這項資訊有幫助嗎?
需要更多協助嗎?聯繋技術支援。