使用APScheduler3.0.1 實(shí)現(xiàn)定時(shí)任務(wù)的方法
需求是在某一指定的時(shí)刻執(zhí)行操作
網(wǎng)上的建議多為通過(guò)調(diào)用Scheduler的add_date_job實(shí)現(xiàn)
不過(guò)APScheduler 3.0.1與之前差異較大, 無(wú)法通過(guò)上述方法實(shí)現(xiàn)
參考 https://apscheduler.readthedocs.org/en/latest/userguide.html APScheduler 3.0.1的userguide 解決問(wèn)題
from datetime import datetime import time import os from apscheduler.schedulers.background import BackgroundScheduler def tick(): print('Tick! The time is: %s' % datetime.now()) if __name__ == '__main__': scheduler = BackgroundScheduler() scheduler.add_job(tick, 'interval', seconds=3) scheduler.start() print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C')) try: # This is here to simulate application activity (which keeps the main thread alive). while True: time.sleep(2) except (KeyboardInterrupt, SystemExit): scheduler.shutdown() # Not strictly necessary if daemonic mode is enabled but should be done if possible
實(shí)例的代碼實(shí)現(xiàn)每3秒執(zhí)行一次tick方法,雖然與需求不符,但發(fā)現(xiàn)add_interval_job在APScheduler 3.0.1中 已經(jīng)被
scheduler.add_job(tick, 'interval', seconds=3)
取代。
help(scheduler.add_job)得到
add_job(func, trigger=None, args=None, kwargs=None, id=None, name=None, misfire_grace_time=undefined, coalesce=undefined, max_instances=undefined, next_run_time=undefined, jobstore='default', executor='default', replace_existing=False, **trigger_args) Adds the given job to the job list and wakes up the scheduler if it's already running. Any option that defaults to undefined will be replaced with the corresponding default value when the job is scheduled (which happens when the scheduler is started, or immediately if the scheduler is already running). The func argument can be given either as a callable object or a textual reference in the package.module:some.object format, where the first half (separated by :) is an importable module and the second half is a reference to the callable object, relative to the module. The trigger argument can either be: the alias name of the trigger (e.g. date, interval or cron), in which case any extra keyword arguments to this method are passed on to the trigger's constructor an instance of a trigger class
由此可知,第參數(shù)為trigger,可取值為 date、interval、cron, **trigger_args為該trigger的構(gòu)造函數(shù)。
通過(guò)源碼找到DateTrigger 的構(gòu)造函數(shù)
def __init__(self, run_date=None, timezone=None)
所以,只需將指定的時(shí)間傳入add_job
scheduler.add_job(tick, 'date', run_date='2014-11-11 14:48:00')
以上這篇使用APScheduler3.0.1 實(shí)現(xiàn)定時(shí)任務(wù)的方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
python+opencv實(shí)現(xiàn)目標(biāo)跟蹤過(guò)程
這篇文章主要介紹了python+opencv實(shí)現(xiàn)目標(biāo)跟蹤過(guò)程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06Python入門(mén)教程(三十九)Python的NumPy安裝與入門(mén)
這篇文章主要介紹了Python入門(mén)教程(三十九)Python的NumPy安裝與入門(mén),NumPy 是一個(gè)Python包,它是一個(gè)由多維數(shù)組對(duì)象和用于處理數(shù)組的例程集合組成的庫(kù),,需要的朋友可以參考下2023-05-05總結(jié)python 三種常見(jiàn)的內(nèi)存泄漏場(chǎng)景
這篇文章主要介紹了總結(jié)python 三種常見(jiàn)的內(nèi)存泄漏場(chǎng)景,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2020-11-11Python pyecharts實(shí)現(xiàn)繪制中國(guó)地圖的實(shí)例詳解
pyecharts是一個(gè)用于生成 Echarts 圖表的類庫(kù)。Echarts 是百度開(kāi)源的一個(gè)數(shù)據(jù)可視化 JS 庫(kù)。用 Echarts 生成的圖可視化效果非常棒。本文將通過(guò)pyecharts繪制中國(guó)地圖,需要的可以學(xué)習(xí)一下2022-01-01Python爬蟲(chóng)小例子——爬取51job發(fā)布的工作職位
這篇文章主要介紹了Python爬取51job發(fā)布的工作職位,文中講解非常細(xì)致,幫助大家更好的理解和學(xué)習(xí)Python,感興趣的朋友可以了解下2020-07-07淺談pandas篩選出表中滿足另一個(gè)表所有條件的數(shù)據(jù)方法
今天小編就為大家分享一篇淺談pandas篩選出表中滿足另一個(gè)表所有條件的數(shù)據(jù)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-02-02基于python實(shí)現(xiàn)對(duì)文件進(jìn)行切分行
這篇文章主要介紹了基于python實(shí)現(xiàn)對(duì)文件進(jìn)行切分行,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04python獲取指定時(shí)間差的時(shí)間實(shí)例詳解
這篇文章主要介紹了python獲取指定時(shí)間差的時(shí)間實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-04-04python PIL Image 圖像處理基本操作實(shí)例
這篇文章主要介紹了python PIL Image 圖像處理基本操作實(shí)例包括圖片加載、灰度圖,圖像通道分離和合并,在圖像上輸出文字,圖像縮放,圖像閾值分割、 二值化,圖像裁剪需要的朋友可以參考下2022-04-04Python爬蟲(chóng)抓取論壇關(guān)鍵字過(guò)程解析
這篇文章主要介紹了Python爬蟲(chóng)抓取論壇關(guān)鍵字過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10