ITPub博客

首页 > 应用开发 > IT综合 > python 调度框架 apscheduler

python 调度框架 apscheduler

IT综合 作者:G8bao7 时间:2016-12-13 15:55:23 0 删除 编辑

安装
    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)
}


参考: 
http://debugo.com/apscheduler/


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/26250550/viewspace-2130398/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2013-11-04

  • 博文量
    496
  • 访问量
    1258309