APScheduler API -- apscheduler.triggers.cron
apscheduler.triggers.cron
API
Trigger alias for add_job(): cron
- class apscheduler.triggers.cron.CronTrigger(year=None, month=None, day=None, week=None, day_of_week=None, hour=None, minute=None, second=None, start_date=None, end_date=None, timezone=None)
-
Bases: apscheduler.triggers.base.BaseTrigger
Triggers when current time matches all specified time constraints, similarly to how the UNIX cron scheduler works.
Parameters: - 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)
Note
The first weekday is always monday.
Introduction
This is the most powerful of the built-in triggers 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 and ending dates for the cron-style schedule through the start_date and end_date parameters, respectively. They can be given as a date/datetime object or text (in the ISO 8601 format).
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, day=1, minute=20 is equivalent to year='*', month='*', day=1, week='*', day_of_week='*', hour='*', minute=20, second=0. The job will then 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 *.
Expression types
The following table lists all the available expressions for use in the fields from year to second. Multiple expression can be given in a single field, separated by commas.
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 |
Daylight saving time behavior
The cron trigger works with the so-called “wall clock” time. Thus, if the selected time zone observes DST (daylight saving time), you should be aware that it may cause unexpected behavior with the cron trigger when entering or leaving DST. When switching from standard time to daylight saving time, clocks are moved either one hour or half an hour forward, depending on the time zone. Likewise, when switching back to standard time, clocks are moved one hour or half an hour backward. This will cause some time periods to either not exist at all, or be repeated. If your schedule would have the job executed on either one of these periods, it may execute more often or less often than expected. This is not a bug. If you wish to avoid this, either use a timezone that does not observe DST, for instance UTC. Alternatively, just find out about the DST switch times and avoid them in your scheduling.
For example, the following schedule may be problematic:
# In the Europe/Helsinki timezone, this will not execute at all on the last sunday morning of March
# Likewise, it will execute twice on the last sunday morning of October
sched.add_job(job_function, 'cron', hour=3, minute=30)
Examples
from apscheduler.scheduler import BlockingScheduler def job_function():
print "Hello World" sched = BlockingScheduler() # 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_job(job_function, 'cron', month='6-8,11-12', day='3rd fri', hour='0-3') sched.start()
You can use start_date and end_date to limit the total time in which the schedule runs:
# Runs from Monday to Friday at 5:30 (am) until 2014-05-30 00:00:00
sched.add_job(job_function, 'cron', day_of_week='mon-fri', hour=5, minute=30, end_date='2014-05-30')
The scheduled_job() decorator works nicely too:
@sched.scheduled_job('cron', id='my_job_id', day='last sun')
def some_decorated_task():
print("I am printed at 00:00:00 on the last Sunday of every month!")
APScheduler API -- apscheduler.triggers.cron的更多相关文章
- APScheduler API -- apscheduler.triggers.interval
apscheduler.triggers.interval API Trigger alias for add_job(): interval class apscheduler.triggers.i ...
- APScheduler API -- apscheduler.triggers.date
apscheduler.triggers.date API Trigger alias for add_job(): date class apscheduler.triggers.date.Date ...
- APScheduler API -- apscheduler.schedulers.base
apscheduler.schedulers.base API class apscheduler.schedulers.base.BaseScheduler(gconfig={}, **option ...
- Python定时任务框架APScheduler 3.0.3 Cron示例
APScheduler是基于Quartz的一个Python定时任务框架,实现了Quartz的所有功能,使用起来十分方便.提供了基于日期.固定时间间隔以及crontab类型的任务,并且可以持久化任务.基 ...
- APScheduler —— Python化的Cron
APScheduler全程为Advanced Python Scheduler,是一款轻量级的Python任务调度框架.它允许你像Cron那样安排定期执行的任务,并且支持Python函数或任意可调用的 ...
- 定时任务APScheduler
安装 APScheduler $ pip install apscheduler 快速开始 from apscheduler.schedulers.blocking import BlockingSc ...
- Python 定时任务框架 APScheduler 详解
APScheduler 最近想写个任务调度程序,于是研究了下 Python 中的任务调度工具,比较有名的是:Celery,RQ,APScheduler. Celery:非常强大的分布式任务调度框架 R ...
- python APScheduler模块
简介 一般来说Celery是python可以执行定时任务, 但是不支持动态添加定时任务 (Django有插件可以动态添加), 而且对于不需要Celery的项目, 就会让项目变得过重. APSchedu ...
- python3使用pyinstaller打包apscheduler出的错
本来只是想用Python做一个定时任务小工具在服务器上运行,可是服务器在隔离区,各种禁止上外网,使用pip导出列表那种下载库的方法不管用,导致Python的各种库都下不到,官网离线下载又各种缺依赖,好 ...
随机推荐
- HDU4240_Route Redundancy
题目很简单.给一个有向图,求两点间的最大流量与任意一条路中的最大流量的比值. 最大流不说了,求出单条流量最大的路径可以用类似Spfa的方法来搞,保存到达当前点的最大流量,一直往下更新即可. 召唤代码君 ...
- python beautifulsoup/xpath/re详解
自己在看python处理数据的方法,发现一篇介绍比较详细的文章 转自:http://blog.csdn.net/lingojames/article/details/72835972 20170531 ...
- How Many Points? LightOJ - 1077(线段经过整点个数与gcd 证明)
题意: 已知两点 (x1,y1) 和 (x2, y2)求两点间线段上的整点的个数 解析: 就是求gcd(abs(x2- x1),abs(y2 - y1)) 证明: 我们分水平方向和竖直方向两个方向看 ...
- c++11 闭包的实现
c++11 闭包的实现 什么是闭包 闭包有很多种定义,一种说法是,闭包是带有上下文的函数.说白了,就是有状态的函数.更直接一些,不就是个类吗?换了个名字而已. 一个函数,带上了一个状态,就变成了闭包了 ...
- [洛谷P4819][中山市选]杀人游戏
题目大意:有一张$n$个点$m$条边的有向图,有一个关键点,如果你访问一个点,你会知道它连出的边中有没有关键点,以及若有的话是哪个.问最优策略下不访问关键点而知道关键点的概率 题解:发现若一个点不是关 ...
- 主机 & 虚拟机 & 开发板 三者的恩爱情仇
# 主机 & 虚拟机 & 开发板 > 三者网络连通性,使用ping命令检测 @ Bridge 模式 ## 主机 & 虚拟机 主机与虚拟机相当于一个网络里的两台主机,都有各 ...
- BZOJ2217 [Poi2011]Lollipop 【贪心】
题目链接 BZOJ2217 题解 如果只判定存不存在方案的话,我倒是想到可以将\(2\)拆成两个\(1\),其中一个不能作为区间开头,线段树优化计算补集方案数 但是一看这道题要输出方案啊,,, 怎么办 ...
- 【bzoj1937】 Shoi2004—Mst 最小生成树
http://www.lydsy.com/JudgeOnline/problem.php?id=1937 (题目链接) 题意 一个无向图,给出一个生成树,可以修改每条边的权值,问最小修改多少权值使得给 ...
- 使用IPMI控制/监控Linux服务器
1 IPMI简述 IPMI提供了很多丰富功能,我使用的功能,说得大白话一点,就是: 1.获取本设备的硬件信息:包括CPU和主板的温度.电压.风扇转速. 2.在设备A上,通过命令,控制远程设 ...
- Python 类编码风格
1.命名 类名:(1)单词首字母均大写 (2)不使用下划线 实例名+模块名:(1)小写格式 (2)下划线分隔单词 2.文档字符串 三引号:“““ ””” 每个类定义后面需要包含一个文档字符串,描述类的 ...