定时任务框架APScheduler学习详解
?
定時(shí)任務(wù)框架APScheduler學(xué)習(xí)詳解
APScheduler簡(jiǎn)介
在平常的工作中幾乎有一半的功能模塊都需要定時(shí)任務(wù)來(lái)推動(dòng),例如項(xiàng)目中有一個(gè)定時(shí)統(tǒng)計(jì)程序,定時(shí)爬出網(wǎng)站的URL程序,定時(shí)檢測(cè)釣魚網(wǎng)站的程序等等,都涉及到了關(guān)于定時(shí)任務(wù)的問(wèn)題,第一時(shí)間想到的是利用time模塊的time.sleep()方法使程序休眠來(lái)達(dá)到定時(shí)任務(wù)的目的,雖然這樣也可以,但是總覺得不是那么的專業(yè),^_^所以就找到了python的定時(shí)任務(wù)模塊APScheduler:
APScheduler基于Quartz的一個(gè)Python定時(shí)任務(wù)框架,實(shí)現(xiàn)了Quartz的所有功能,使用起來(lái)十分方便。提供了基于日期、固定時(shí)間間隔以及crontab類型的任務(wù),并且可以持久化任務(wù)。基于這些功能,我們可以很方便的實(shí)現(xiàn)一個(gè)python定時(shí)任務(wù)系統(tǒng)。
安裝
1、利用pip進(jìn)行安裝
| 1 | $ pip install apscheduler |
2、源碼安裝(https://pypi.python.org/pypi/APScheduler/)
| 1 | $ python setup.py install |
APScheduler有四種組成部分:
觸發(fā)器(trigger)包含調(diào)度邏輯,每一個(gè)作業(yè)有它自己的觸發(fā)器,用于決定接下來(lái)哪一個(gè)作業(yè)會(huì)運(yùn)行。除了他們自己初始配置意外,觸發(fā)器完全是無(wú)狀態(tài)的。
作業(yè)存儲(chǔ)(job store)存儲(chǔ)被調(diào)度的作業(yè),默認(rèn)的作業(yè)存儲(chǔ)是簡(jiǎn)單地把作業(yè)保存在內(nèi)存中,其他的作業(yè)存儲(chǔ)是將作業(yè)保存在數(shù)據(jù)庫(kù)中。一個(gè)作業(yè)的數(shù)據(jù)講在保存在持久化作業(yè)存儲(chǔ)時(shí)被序列化,并在加載時(shí)被反序列化。調(diào)度器不能分享同一個(gè)作業(yè)存儲(chǔ)。
執(zhí)行器(executor)處理作業(yè)的運(yùn)行,他們通常通過(guò)在作業(yè)中提交制定的可調(diào)用對(duì)象到一個(gè)線程或者進(jìn)城池來(lái)進(jìn)行。當(dāng)作業(yè)完成時(shí),執(zhí)行器將會(huì)通知調(diào)度器。
調(diào)度器(scheduler)是其他的組成部分。你通常在應(yīng)用只有一個(gè)調(diào)度器,應(yīng)用的開發(fā)者通常不會(huì)直接處理作業(yè)存儲(chǔ)、調(diào)度器和觸發(fā)器,相反,調(diào)度器提供了處理這些的合適的接口。配置作業(yè)存儲(chǔ)和執(zhí)行器可以在調(diào)度器中完成,例如添加、修改和移除作業(yè)。
簡(jiǎn)單應(yīng)用:
| 1 2 3 4 5 6 7 8 9 | 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è)
上面是通過(guò)add_job()來(lái)添加作業(yè),另外還有一種方式是通過(guò)scheduled_job()修飾器來(lái)修飾函數(shù)
| 1 2 3 4 5 6 7 8 9 10 | 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è)
| 1 2 3 4 5 | job?=?scheduler.add_job(myfunc,?'interval', minutes=2) job.remove() #如果有多個(gè)任務(wù)序列的話可以給每個(gè)任務(wù)設(shè)置ID號(hào),可以根據(jù)ID號(hào)選擇清除對(duì)象,且remove放到start前才有效 sched.add_job(myfunc,?'interval', minutes=2,?id='my_job_id') sched.remove_job('my_job_id') |
3. 暫停和恢復(fù)作業(yè)
?暫停作業(yè):
| 1 2 | apsched.job.Job.pause() apsched.schedulers.base.BaseScheduler.pause_job() |
?恢復(fù)作業(yè):
| 1 2 | apsched.job.Job.resume() apsched.schedulers.base.BaseScheduler.resume_job() |
4. 獲得job列表
?獲得調(diào)度作業(yè)的列表,可以使用get_jobs()來(lái)完成,它會(huì)返回所有的job實(shí)例。或者使用print_jobs()來(lái)輸出所有格式化的作業(yè)列表。也可以利用get_job(任務(wù)ID)獲取指定任務(wù)的作業(yè)列表
| 1 2 3 | 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)度器會(huì)等待所有正在運(yùn)行的作業(yè)完成后,關(guān)閉所有的調(diào)度器和作業(yè)存儲(chǔ)。如果你不想等待,可以將wait選項(xiàng)設(shè)置為False。
| 1 2 | sched.shutdown() sched.shutdown(wait=False) |
?作業(yè)運(yùn)行的控制(trigger)
add_job的第二個(gè)參數(shù)是trigger,它管理著作業(yè)的調(diào)度方式。它可以為date, interval或者cron。對(duì)于不同的trigger,對(duì)應(yīng)的參數(shù)也相同。
(1). cron定時(shí)調(diào)度(某一定時(shí)時(shí)刻執(zhí)行)
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 | (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表示也可以用其英語(yǔ)縮寫表示) 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ù)的取值格式:
例子:
| 1 2 3 4 5 6 7 8 9 10 11 | #表示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í)行)
| 1 2 3 4 5 6 7 8 | 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 |
例子:
| 1 2 | #表示每隔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è)只會(huì)執(zhí)行一次)
| 1 2 | 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 |
例子:
| 1 2 3 4 | # 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']) |
總結(jié)
以上是生活随笔為你收集整理的定时任务框架APScheduler学习详解的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。