apscheduler mysql_APScheduler (重点)
定時校正
需求: mysql和redis兩個系統(tǒng), mysql增加數(shù)據(jù)成功, redis未必添加成功, 這樣兩個系統(tǒng)的數(shù)據(jù)可能出現(xiàn)偏差, 所以需要定期對mysql和redis的數(shù)據(jù)進(jìn)行同步
解決方案: 每天執(zhí)行一次定時任務(wù), 讓mysql數(shù)據(jù)和redis數(shù)據(jù)進(jìn)行同步
crontab
是linux系統(tǒng)一個內(nèi)置命令, 依賴于linux系統(tǒng), 無動態(tài)管理任務(wù)(取消/暫停/修改任務(wù)配置)
使用場景: 適合于普通的靜態(tài)任務(wù)
apscheduler
獨(dú)立的定時器程序, 可以方便的管理定時任務(wù)
使用場景: 需要動態(tài)生成/管理任務(wù), 如下單后30分鐘可有效期
安裝 pip install apscheduler
支持三種觸發(fā)器
date 只執(zhí)行一次
interval 周期執(zhí)行 參數(shù) 時間間隔
cron 周期執(zhí)行 參數(shù) 時間
調(diào)度器 Scheduler
負(fù)責(zé)管理定時任務(wù)
BlockingScheduler: 作為獨(dú)立進(jìn)程時使用
from apscheduler.schedulers.blocking import BlockingScheduler
scheduler = BlockingScheduler()
scheduler.start() # 此處程序會發(fā)生阻塞
BackgroundScheduler: 在框架程序(如Django、Flask)中使用
from apscheduler.schedulers.background import BackgroundScheduler
scheduler = BackgroundScheduler()
scheduler.start() # 此處程序不會發(fā)生阻塞
執(zhí)行器 executors
在定時任務(wù)該執(zhí)行時,以進(jìn)程或線程方式執(zhí)行任務(wù)
ThreadPoolExecutor
from apscheduler.executors.pool import ThreadPoolExecutor
ThreadPoolExecutor(max_workers)
ThreadPoolExecutor(20) # 最多20個線程同時執(zhí)行
使用方法
executors = {
'default': ThreadPoolExecutor(20)
}
scheduler = BackgroundScheduler(executors=executors)
ProcessPoolExecutor
from apscheduler.executors.pool import ProcessPoolExecutor
ProcessPoolExecutor(max_workers)
ProcessPoolExecutor(5) # 最多5個進(jìn)程同時執(zhí)行
使用方法
executors = {
'default': ProcessPoolExecutor(3)
}
scheduler = BackgroundScheduler(executors=executors)
觸發(fā)器 Trigger
指定定時任務(wù)執(zhí)行的時機(jī)
1) date 在特定的時間日期執(zhí)行
from datetime import date
# 在2019年11月6日00:00:00執(zhí)行
sched.add_job(my_job, 'date', run_date=date(2009, 11, 6))
# 在2019年11月6日16:30:05
sched.add_job(my_job, 'date', run_date=datetime(2009, 11, 6, 16, 30, 5))
sched.add_job(my_job, 'date', run_date='2009-11-06 16:30:05')
# 立即執(zhí)行
sched.add_job(my_job, 'date')
sched.start()
2) interval 經(jīng)過指定的時間間隔執(zhí)行
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
from datetime import datetime
# 每兩小時執(zhí)行一次
sched.add_job(job_function, 'interval', hours=2)
# 在2010年10月10日09:30:00 到2014年6月15日的時間內(nèi),每兩小時執(zhí)行一次
sched.add_job(job_function, 'interval', hours=2, start_date='2010-10-10 09:30:00', end_date='2014-06-15 11:00:00')
3) cron 按指定的周期執(zhí)行
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)
# 在6、7、8、11、12月的第三個周五的00:00, 01:00, 02:00和03:00 執(zhí)行
sched.add_job(job_function, 'cron', month='6-8,11-12', day='3rd fri', hour='0-3')
# 在2014年5月30日前的周一到周五的5:30執(zhí)行
sched.add_job(job_function, 'cron', day_of_week='mon-fri', hour=5, minute=30, end_date='2014-05-30')
任務(wù)管理
方式1
job = scheduler.add_job(myfunc, 'interval', minutes=2) # 添加任務(wù)
job.remove() # 刪除任務(wù)
job.pause() # 暫定任務(wù)
job.resume() # 恢復(fù)任務(wù)
方式2
scheduler.add_job(myfunc, 'interval', minutes=2, id='my_job_id') # 添加任務(wù)
scheduler.remove_job('my_job_id') # 刪除任務(wù)
scheduler.pause_job('my_job_id') # 暫定任務(wù)
scheduler.resume_job('my_job_id') # 恢復(fù)任務(wù)
調(diào)整任務(wù)調(diào)度周期
job.modify(max_instances=6, name='Alternate name')
scheduler.reschedule_job('my_job_id', trigger='cron', minute='*/5')
停止APScheduler運(yùn)行
scheduler.shutdown()
代碼
import time
from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.executors.pool import ThreadPoolExecutor
# 創(chuàng)建執(zhí)行器 用于支持多進(jìn)程/多線程
executor = ThreadPoolExecutor(max_workers=5)
# 創(chuàng)建調(diào)度器
scheduler = BackgroundScheduler(executors={'default': executor})
def func1(name, age):
print(name, age)
# 添加任務(wù)
# date 只執(zhí)行一次
# scheduler.add_job(func1, "date", run_date='2019-08-29 14:53:40', args=['zs', 30])
# interval 周期執(zhí)行 參數(shù)是時間間隔
# scheduler.add_job(func1, "interval", seconds=10, args=['zs', 30])
# cron 周期執(zhí)行 參數(shù)是時間 每月1號3點(diǎn)會執(zhí)行一次
# scheduler.add_job(func1, "cron", day=1, hour=3, args=['zs', 30])
# 秒針每次到30時執(zhí)行一次
scheduler.add_job(func1, "cron", second=30, args=['zs', 30])
# 啟動調(diào)度器
scheduler.start()
while True:
time.sleep(24 * 60 * 60)
總結(jié)
以上是生活随笔為你收集整理的apscheduler mysql_APScheduler (重点)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python 利用cip.cc查询IP归
- 下一篇: qpsk 锁相环_本科毕业设计课题—QP