Python的任务调度模块APScheduler学习3(作业运行控制-trigger)

来源:互联网 发布:javascript 格式化 编辑:程序博客网 时间:2024/06/05 07:01
add_job的第二个参数是trigger,它管理着作业的调度方式。它可以为date, interval或者cron。对于不同的trigger,对应的参数也不相同。
1、 cron定时调度(某一定时时刻执行)
(int|str) 表示参数既可以是int类型,也可以是str类型(datetime | str) 表示参数既可以是datetime类型,也可以是str类型year (int|str) – 4-digit year -(表示四位数的年份,如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(扩展形式)或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时)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) - (表示开始时间)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) -(表示时区取值)

参数的取值格式:
Expression FieldDescription
* 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

例子:
#表示2017年3月22日17时19分07秒执行该程序sched.add_job(my_job, 'cron', year=2017,month = 03,day = 22,hour = 17,minute = 19,second = 07) #表示任务在6,7,8,11,12月份的第三个星期五的00:00,01:00,02:00,03:00 执行该程序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:00sched.add_job(my_job(), 'cron', day_of_week='mon-fri', hour=5, minute=30,end_date='2014-05-30') #表示每5秒执行该程序一次,相当于interval 间隔调度中seconds = 5sched.add_job(my_job, 'cron',second = '*/5')

2、 interval 间隔调度(每隔多久执行)
weeks (int) – number of weeks to waitdays (int) – number of days to waithours (int) – number of hours to waitminutes (int) – number of minutes to waitseconds (int) – number of seconds to waitstart_date (datetime|str) – starting point for the interval calculationend_date (datetime|str) – latest possible date/time to trigger ontimezone (datetime.tzinfo|str) – time zone to use for the date/time calculations

例子:
#表示每隔3天17时19分07秒执行一次任务sched.add_job(my_job, 'interval',days  = 03,hours = 17,minutes = 19,seconds = 07)

3、 date 定时调度(作业只会执行一次)
run_date (datetime|str) – the date/time to run the job at  -(任务开始的时间)timezone (datetime.tzinfo|str) – time zone for run_date if it doesn’t have one already

例子:
# The job will be executed on November 6th, 2009sched.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:05sched.add_job(my_job, 'date', run_date=datetime(2009, 11, 6, 16, 30, 5), args=['text'])


阅读全文
0 0
原创粉丝点击