python 排程框架 apscheduler

G8bao7發表於2016-12-13

安裝
    pip install apschedule

APScheduler中四個元件:
排程器(scheduler)是其他的組成部分。你通常在應用只有一個排程器,
應用的開發者通常不會直接處理作業儲存、排程器和觸發器,相反,排程器提供了處理這些的合適的介面。
配置作業儲存和執行器可以在排程器中完成,例如新增、修改和移除作業
BlockingScheduler: 阻塞式,當排程器是你應用中唯一要執行的東西時使用。
BackgroundScheduler: 非阻塞式,當你不執行任何其他框架時使用,並希望排程器在你應用的後臺執行。
觸發器(trigger)包含排程邏輯,每一個Job必須定義自己的觸發器。
Job的排程方式: date, interval, cron。不同的trigger對應不同引數
作業儲存(job store)儲存被排程的作業, 預設是MemoryJobStore
預設的作業儲存是簡單地把作業儲存在記憶體中,其他的作業儲存是將作業儲存在資料庫中。
一個作業的資料在持久化作業儲存時被序列化,並在載入時被反序列化。排程器不能分享同一個作業儲存。
執行器(executor)處理作業的執行, 預設是ThreadPoolExecutor,其中執行緒池的最大執行緒數為10
他們通常透過在作業中提交制定的可呼叫物件到一個執行緒或者進城池來進行。當作業完成時,執行器將會通知排程器。


> Example:

### scheduler 初始化 ###
# 阻塞式
scheduler = BlockingScheduler(jobstores=jobstores, executors=executors, job_defaults=job_defaults)
# 非阻塞式
scheduler = BackgroundScheduler(jobstores=jobstores, executors=executors, job_defaults=job_defaults)

scheduler.start()
scheduler.shutdown()
# 不等待正在執行的作業
scheduler.shutdown(wait=False)

### Job ###
job_defaults = {
    'coalesce': False,
    'max_instances': 3
}
scheduler.add_job(func, trigger, args=None, kwargs=None, id=None, name=None, next_run_time=undefined, jobstore='default', executor='default',replace_existing=False, **trigger_args)
scheduler.get_jobs()
scheduler.print_jobs()
scheduler.remove_all_jobs()
scheduler.pause()
scheduler.pause_job(job_id, jobstore)

# trigger #
# date 引數
# run_date (datetime|str) – the date/time to run the job at
scheduler.add_job(func=job_function, trigger='date', run_date='2016-12-13 14:21:05', jobstore="mongo")

# interval 引數
# 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
scheduler.add_job(func=job_function, args=[], trigger='interval', minutes=2)

# cron 引數
# 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)
# 和Linux的Crontab一樣,它的值格式為:
# 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

# Schedules job_function to be run on the third Friday
# of June, July, August, November and December at 00:00, 01:00, 02:00 and 03:00
scheduler.add_job(func=job_function, trigger='cron', month='6-8,11-12', day='3rd fri', hour='0-3')
# Runs from Monday to Friday at 5:30 (am) until 2014-05-30 00:00:00
scheduler.add_job(func=job_function, trigger='cron', day_of_week='mon-fri', hour=5, minute=30, end_date='2014-05-30')

### 定義作業儲存 ###
from apscheduler.jobstores.mongodb import MongoDBJobStore
from apscheduler.jobstores.memory import MemoryJobStore
jobstores = {
    'mongo': MongoDBJobStore(collection='job', database=mongodb_dbname, client=mongodb_client),
    'default': MemoryJobStore()
}
### 定義執行器 ###
executors = {
    'default': ThreadPoolExecutor(10),
    'processpool': ProcessPoolExecutor(3)
}


參考: 



來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26250550/viewspace-2130398/,如需轉載,請註明出處,否則將追究法律責任。

相關文章