Python定時(shí)任務(wù)框架APScheduler原理及常用代碼
APScheduler簡介
在平常的工作中幾乎有一半的功能模塊都需要定時(shí)任務(wù)來推動(dòng),例如項(xiàng)目中有一個(gè)定時(shí)統(tǒng)計(jì)程序,定時(shí)爬出網(wǎng)站的URL程序,定時(shí)檢測釣魚網(wǎng)站的程序等等,都涉及到了關(guān)于定時(shí)任務(wù)的問題,第一時(shí)間想到的是利用time模塊的time.sleep()方法使程序休眠來達(dá)到定時(shí)任務(wù)的目的,雖然這樣也可以,但是總覺得不是那么的專業(yè),^_^所以就找到了python的定時(shí)任務(wù)模塊APScheduler:
APScheduler基于Quartz的一個(gè)Python定時(shí)任務(wù)框架,實(shí)現(xiàn)了Quartz的所有功能,使用起來十分方便。提供了基于日期、固定時(shí)間間隔以及crontab類型的任務(wù),并且可以持久化任務(wù)。基于這些功能,我們可以很方便的實(shí)現(xiàn)一個(gè)python定時(shí)任務(wù)系統(tǒng)。
安裝
1、利用pip進(jìn)行安裝
$ pip install apscheduler
2、源碼安裝(https://pypi.python.org/pypi/APScheduler/)
$ python setup.py install
APScheduler有四種組成部分:
觸發(fā)器(trigger)包含調(diào)度邏輯,每一個(gè)作業(yè)有它自己的觸發(fā)器,用于決定接下來哪一個(gè)作業(yè)會運(yùn)行。除了他們自己初始配置意外,觸發(fā)器完全是無狀態(tài)的。
作業(yè)存儲(job store)存儲被調(diào)度的作業(yè),默認(rèn)的作業(yè)存儲是簡單地把作業(yè)保存在內(nèi)存中,其他的作業(yè)存儲是將作業(yè)保存在數(shù)據(jù)庫中。一個(gè)作業(yè)的數(shù)據(jù)講在保存在持久化作業(yè)存儲時(shí)被序列化,并在加載時(shí)被反序列化。調(diào)度器不能分享同一個(gè)作業(yè)存儲。
執(zhí)行器(executor)處理作業(yè)的運(yùn)行,他們通常通過在作業(yè)中提交制定的可調(diào)用對象到一個(gè)線程或者進(jìn)城池來進(jìn)行。當(dāng)作業(yè)完成時(shí),執(zhí)行器將會通知調(diào)度器。
調(diào)度器(scheduler)是其他的組成部分。你通常在應(yīng)用只有一個(gè)調(diào)度器,應(yīng)用的開發(fā)者通常不會直接處理作業(yè)存儲、調(diào)度器和觸發(fā)器,相反,調(diào)度器提供了處理這些的合適的接口。配置作業(yè)存儲和執(zhí)行器可以在調(diào)度器中完成,例如添加、修改和移除作業(yè)?!?/p>
簡單應(yīng)用:
import time
from apscheduler.schedulers.blocking import BlockingScheduler
def my_job():
print time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
sched = BlockingScheduler()
sched.add_job(my_job, 'interval', seconds=5)
sched.start()
上面的例子表示每隔5s執(zhí)行一次my_job函數(shù),輸出當(dāng)前時(shí)間信息
操作作業(yè)
1. 添加作業(yè)
上面是通過add_job()來添加作業(yè),另外還有一種方式是通過scheduled_job()修飾器來修飾函數(shù)
import time
from apscheduler.schedulers.blocking import BlockingScheduler
sched = BlockingScheduler()
@sched.scheduled_job('interval', seconds=5)
def my_job():
print time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
sched.start()
2. 移除作業(yè)
job = scheduler.add_job(myfunc, 'interval', minutes=2)
job.remove()
#如果有多個(gè)任務(wù)序列的話可以給每個(gè)任務(wù)設(shè)置ID號,可以根據(jù)ID號選擇清除對象,且remove放到start前才有效
sched.add_job(myfunc, 'interval', minutes=2, id='my_job_id')
sched.remove_job('my_job_id')
3. 暫停和恢復(fù)作業(yè)
暫停作業(yè):
apsched.job.Job.pause()
apsched.schedulers.base.BaseScheduler.pause_job()
恢復(fù)作業(yè):
apsched.job.Job.resume()
apsched.schedulers.base.BaseScheduler.resume_job()
4. 獲得job列表
獲得調(diào)度作業(yè)的列表,可以使用get_jobs()來完成,它會返回所有的job實(shí)例?;蛘呤褂胮rint_jobs()來輸出所有格式化的作業(yè)列表。也可以利用get_job(任務(wù)ID)獲取指定任務(wù)的作業(yè)列表
job = sched.add_job(my_job, 'interval', seconds=2 ,id='123')
print sched.get_job(job_id='123')
print sched.get_jobs()
5. 關(guān)閉調(diào)度器
默認(rèn)情況下調(diào)度器會等待所有正在運(yùn)行的作業(yè)完成后,關(guān)閉所有的調(diào)度器和作業(yè)存儲。如果你不想等待,可以將wait選項(xiàng)設(shè)置為False。
sched.shutdown()
sched.shutdown(wait=False)
作業(yè)運(yùn)行的控制(trigger)
add_job的第二個(gè)參數(shù)是trigger,它管理著作業(yè)的調(diào)度方式。它可以為date, interval或者cron。對于不同的trigger,對應(yīng)的參數(shù)也相同。
(1). cron定時(shí)調(diào)度(某一定時(shí)時(shí)刻執(zhí)行)
(int|str) 表示參數(shù)既可以是int類型,也可以是str類型
(datetime | str) 表示參數(shù)既可以是datetime類型,也可以是str類型
year (int|str) – 4-digit year -(表示四位數(shù)的年份,如2008年)
month (int|str) – month (1-12) -(表示取值范圍為1-12月)
day (int|str) – day of the (1-31) -(表示取值范圍為1-31日)
week (int|str) – ISO week (1-53) -(格里歷2006年12月31日可以寫成2006年-W52-7(擴(kuò)展形式)或2006W527(緊湊形式))
day_of_week (int|str) – number or name of weekday (0-6 or mon,tue,wed,thu,fri,sat,sun) - (表示一周中的第幾天,既可以用0-6表示也可以用其英語縮寫表示)
hour (int|str) – hour (0-23) - (表示取值范圍為0-23時(shí))
minute (int|str) – minute (0-59) - (表示取值范圍為0-59分)
second (int|str) – second (0-59) - (表示取值范圍為0-59秒)
start_date (datetime|str) – earliest possible date/time to trigger on (inclusive) - (表示開始時(shí)間)
end_date (datetime|str) – latest possible date/time to trigger on (inclusive) - (表示結(jié)束時(shí)間)
timezone (datetime.tzinfo|str) – time zone to use for the date/time calculations (defaults to scheduler timezone) -(表示時(shí)區(qū)取值)
參數(shù)的取值格式:
例子:
#表示2017年3月22日17時(shí)19分07秒執(zhí)行該程序
sched.add_job(my_job, 'cron', year=2017,month = 03,day = 22,hour = 17,minute = 19,second = 07)
#表示任務(wù)在6,7,8,11,12月份的第三個(gè)星期五的00:00,01:00,02:00,03:00 執(zhí)行該程序
sched.add_job(my_job, 'cron', month='6-8,11-12', day='3rd fri', hour='0-3')
#表示從星期一到星期五5:30(AM)直到2014-05-30 00:00:00
sched.add_job(my_job(), 'cron', day_of_week='mon-fri', hour=5, minute=30,end_date='2014-05-30')
#表示每5秒執(zhí)行該程序一次,相當(dāng)于interval 間隔調(diào)度中seconds = 5
sched.add_job(my_job, 'cron',second = '*/5')
(2). interval 間隔調(diào)度(每隔多久執(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
例子:
#表示每隔3天17時(shí)19分07秒執(zhí)行一次任務(wù)
sched.add_job(my_job, 'interval',days = 03,hours = 17,minutes = 19,seconds = 07)
(3). date 定時(shí)調(diào)度(作業(yè)只會執(zhí)行一次)
run_date (datetime|str) – the date/time to run the job at -(任務(wù)開始的時(shí)間)
timezone (datetime.tzinfo|str) – time zone for run_date if it doesn't have one already
例子:
# The job will be executed on November 6th, 2009
sched.add_job(my_job, 'date', run_date=date(2009, 11, 6), args=['text'])
# The job will be executed on November 6th, 2009 at 16:30:05
sched.add_job(my_job, 'date', run_date=datetime(2009, 11, 6, 16, 30, 5), args=['text'])
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- python定時(shí)任務(wù)apscheduler的詳細(xì)使用教程
- Python定時(shí)任務(wù)APScheduler安裝及使用解析
- Python定時(shí)任務(wù)APScheduler原理及實(shí)例解析
- Python使用APScheduler實(shí)現(xiàn)定時(shí)任務(wù)過程解析
- Python定時(shí)任務(wù)工具之APScheduler使用方式
- Python定時(shí)任務(wù)APScheduler的實(shí)例實(shí)例詳解
- Python中定時(shí)任務(wù)框架APScheduler的快速入門指南
- Python使用apscheduler模塊設(shè)置定時(shí)任務(wù)的實(shí)現(xiàn)
相關(guān)文章
Python獲取運(yùn)行目錄與當(dāng)前腳本目錄的方法
這篇文章主要介紹了Python獲取運(yùn)行目錄與當(dāng)前腳本目錄的方法,涉及Python目錄操作與系統(tǒng)相關(guān)變量的獲取技巧,需要的朋友可以參考下2015-06-06解決使用Pandas 讀取超過65536行的Excel文件問題
這篇文章主要介紹了解決使用Pandas 讀取超過65536行的Excel文件問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11利用Python制作一個(gè)簡單的天氣播報(bào)系統(tǒng)
最近天氣的多變,好幾次出門半路天氣轉(zhuǎn)變。本文將利用python整個(gè)天氣爬蟲來獲取天氣情況。這樣也好可以進(jìn)行一個(gè)提前預(yù)防,感興趣的可以動(dòng)手試一試2022-05-05一文教你如何用Python輕輕松松操作Excel,Word,CSV
數(shù)據(jù)處理是 Python 的一大應(yīng)用場景,而 Excel 又是當(dāng)前最流行的數(shù)據(jù)處理軟件。本文將為大家詳細(xì)介紹一下如何用Python輕輕松松操作Excel、Word、CSV,需要的可以參考一下2022-02-02Pandas數(shù)據(jù)操作及數(shù)據(jù)分析常用技術(shù)介紹
Pandas是Python中用于數(shù)據(jù)處理和數(shù)據(jù)分析的庫,具有強(qiáng)大的數(shù)據(jù)操作和分析功能,包括數(shù)據(jù)清洗、轉(zhuǎn)換、篩選、聚合等。常用技術(shù)有數(shù)據(jù)讀取與寫入、數(shù)據(jù)索引、數(shù)據(jù)切片、數(shù)據(jù)合并、數(shù)據(jù)透視表、數(shù)據(jù)可視化等,適用于各種數(shù)據(jù)分析和機(jī)器學(xué)習(xí)任務(wù)2023-04-04Django框架會話技術(shù)實(shí)例分析【Cookie與Session】
這篇文章主要介紹了Django框架會話技術(shù),結(jié)合實(shí)例形式分析了Django框架Cookie與Session相關(guān)使用技巧與注意事項(xiàng),需要的朋友可以參考下2019-05-05python GUI庫圖形界面開發(fā)之PyQt5控件數(shù)據(jù)拖曳Drag與Drop詳細(xì)使用方法與實(shí)例
這篇文章主要介紹了python GUI庫圖形界面開發(fā)之PyQt5控件數(shù)據(jù)拖曳Drag與Drop詳細(xì)使用方法與實(shí)例,需要的朋友可以參考下2020-02-02