4.1 APS定时框架
本节主题:4.1 APS定时框架
课程讲师:Charlie
观看地址:点我进入
1 本节要点
- 掌握 APS 定时框架;
2 课前准备
2.1 安装Apscheduler
在cmd(Windows)或终端(Mac)输入:
pip install apscheduler
3 课程内容
3.1 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表示定时任务
3.1.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
3.1.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.1.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 |