python apsheduler cron 参数解析

from:https://apscheduler.readthedocs.io/en/v2.1.2/cronschedule.html

Cron-style scheduling

This is the most powerful scheduling method available in APScheduler. You can specify a variety of different expressions on each field, and when determining the next execution time, it finds the earliest possible time that satisfies the conditions in every field. This behavior resembles the “Cron” utility found in most UNIX-like operating systems.

You can also specify the starting date for the cron-style schedule through the start_date parameter, which can be given as a date/datetime object or text. See the Date-based scheduling section for examples on that.

Unlike with crontab expressions, you can omit fields that you don’t need. Fields greater than the least significant explicitly defined field default to * while lesser fields default to their minimum values except for week and day_of_week which default to *. For example, if you specify onlyday=1, minute=20, then the job will execute on the first day of every month on every year at 20 minutes of every hour. The code examples below should further illustrate this behavior.

Note

The behavior for omitted fields was changed in APScheduler 2.0. Omitted fields previously always defaulted to *.

Available fields

FieldDescription
year4-digit year number
monthmonth number (1-12)
dayday of the month (1-31)
weekISO week number (1-53)
day_of_weeknumber or name of weekday (0-6 or mon,tue,wed,thu,fri,sat,sun)
hourhour (0-23)
minuteminute (0-59)
secondsecond (0-59)

Note

The first weekday is always monday.

Expression types

The following table lists all the available expressions applicable in cron-style schedules.

ExpressionFieldDescription
*anyFire on every value
*/aanyFire every a values, starting from the minimum
a-banyFire on any value within the a-b range (a must be smaller than b)
a-b/canyFire every c values within the a-b range
xth ydayFire on the x -th occurrence of weekday y within the month
last xdayFire on the last occurrence of weekday x within the month
lastdayFire on the last day within the month
x,y,zanyFire on any matching expression; can combine any number of any of the above expressions

Example 1

from apscheduler.scheduler import Scheduler

# Start the scheduler
sched = Scheduler() sched.start() def job_function(): print "Hello World" # 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 sched.add_cron_job(job_function, month='6-8,11-12', day='3rd fri', hour='0-3') 

Example 2

# Initialization similar as above, the backup function defined elsewhere

# Schedule a backup to run once from Monday to Friday at 5:30 (am)
sched.add_cron_job(backup, day_of_week='mon-fri', hour=5, minute=30) 

Decorator syntax

As a convenience, there is an alternative syntax for using cron-style schedules. The cron_schedule()decorator can be attached to any function, and has the same syntax as add_cron_job(), except for the func parameter, obviously.

@sched.cron_schedule(day='last sun') def some_decorated_task(): print "I am printed at 00:00:00 on the last Sunday of every month!" 

If you need to unschedule the decorated functions, you can do it this way:

scheduler.unschedule_job(job_function.job)

转载于:https://www.cnblogs.com/yueyanyu/p/6739565.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值