[Dynamic Language] Python定时任务框架
APScheduler是一个Python定时任务框架,使用起来十分方便。提供了基于日期、固定时间间隔以及crontab类型的任务,并且可以持久化任务、并以daemon方式运行应用。
在APScheduler中有四个组件:
触发器(trigger)包含调度逻辑,每一个作业有它自己的触发器,用于决定接下来哪一个作业会运行。除了他们自己初始配置意外,触发器完全是无状态的。
作业存储(job store)存储被调度的作业,默认的作业存储是简单地把作业保存在内存中,其他的作业存储是将作业保存在数据库中。一个作业的数据讲在保存在持久化作业存储时被序列化,并在加载时被反序列化。调度器不能分享同一个作业存储。

执行器(executor)处理作业的运行,他们通常通过在作业中提交制定的可调用对象到一个线程或者进城池来进行。当作业完成时,执行器将会通知调度器。
调度器(scheduler)是其他的组成部分。你通常在应用只有一个调度器,应用的开发者通常不会直接处理作业存储、调度器和触发器,相反,调度器提供了处理这些的合适的接口。配置作业存储和执行器可以在调度器中完成,例如添加、修改和移除作业。

你需要选择合适的调度器,这取决于你的应用环境和你使用APScheduler的目的。通常最常用的两个:
– BlockingScheduler: 当调度器是你应用中唯一要运行的东西时使用。
– BackgroundScheduler: 当你不运行任何其他框架时使用,并希望调度器在你应用的后台执行。
测试计划任务
mac-abeen:timetask abeen$ vim testtask.py
1 # !/usr/bin/env python
2 # -*- encoding:utf-8 -*-
3
4 from datetime import datetime
5
6
7 class TestTask(object):
8 """
9 测试计划任务
10 """
11
12 def print_file(self, content):
13 f = open("testtask.log", "a")
14 f.write(content)
15 f.close()
16
17
18 def run_second(self):
19 self.print_file("run_second is run on {0} \r\n ".format(datetime.now()))
20
21
22 def run_minute(self):
23 self.print_file("run_minute is run on {0} \r\n ".format(datetime.now()))
24
25
26
27
28 if __name__ == "__main__":
29 tt = TestTask()
30 tt.run_second()
31 tt.run_minute()
任务执行
mac-abeen:timetask abeen$ vim run_task.py
1 # !/usr/bin/env python
2 # -*- encoding:utf-8 -*-
3
4 from datetime import datetime
5 import time
6 from apscheduler.schedulers.blocking import BlockingScheduler
7 from testtask import TestTask
8
9 def run():
10 scheduler = BlockingScheduler()
11 scheduler.add_job(func=TestTask().run_second, trigger='interval', seconds=3)
12 scheduler.add_job(func=TestTask().run_minute, trigger='interval', seconds=60)
13
14 scheduler.start()
15
16
17 if __name__ == "__main__":
18 print "scheduler is run ......"
19 run()
日志记录(测试部分日志信息)
1 run_second is run on 2017-06-29 15:13:16.070406
2 run_minute is run on 2017-06-29 15:13:16.071498
3 run_second is run on 2017-06-29 15:13:32.378192
...
22 run_minute is run on 2017-06-29 15:14:29.379333
23 run_second is run on 2017-06-29 15:14:29.379462
24 run_second is run on 2017-06-29 15:14:32.380961
...
44 run_minute is run on 2017-06-29 15:15:29.380135
45 run_second is run on 2017-06-29 15:15:32.380093
46 run_second is run on 2017-06-29 15:15:35.378075
....
作业运行的控制
add_job的第二个参数是trigger,它管理着作业的调度方式。它可以为date, interval或者cron。对于不同的trigger,对应的参数也相同。
(1). 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 |
几个例子如下:
# The job will be executed on November 6th, 2009
sched.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:05
sched.add_job(my_job, 'date', run_date=datetime(2009, 11, 6, 16, 30, 5), args=['text'])
(2). 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
例子:
# Schedule job_function to be called every two hours
sched.add_job(job_function, 'interval', hours=2)
(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, 2009
sched.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:05
sched.add_job(my_job, 'date', run_date=datetime(2009, 11, 6, 16, 30, 5), args=['text'])
官方资料帮助:
https://pypi.python.org/pypi/APScheduler/#downloads
https://github.com/agronholm/apscheduler
[Dynamic Language] Python定时任务框架的更多相关文章
- Python定时任务框架APScheduler 3.0.3 Cron示例
APScheduler是基于Quartz的一个Python定时任务框架,实现了Quartz的所有功能,使用起来十分方便.提供了基于日期.固定时间间隔以及crontab类型的任务,并且可以持久化任务.基 ...
- Python定时任务框架APScheduler
http://blog.csdn.net/chosen0ne/article/details/7842421 APScheduler是基于Quartz的一个Python定时任务框架,实现了Quartz ...
- [转]Python定时任务框架APScheduler
APScheduler是基于Quartz的 一个Python定时任务框架,实现了Quartz的所有功能,使用起来十分方便.提供了基于日期.固定时间间隔以及crontab类型的任务,并且可以 持久化任务 ...
- Python 定时任务框架 APScheduler 详解
APScheduler 最近想写个任务调度程序,于是研究了下 Python 中的任务调度工具,比较有名的是:Celery,RQ,APScheduler. Celery:非常强大的分布式任务调度框架 R ...
- 分布式定时任务框架——python定时任务框架APScheduler扩展
http://bbs.7boo.org/forum.php?mod=viewthread&tid=14546 如果将定时任务部署在一台服务器上,那么这个定时任务就是整个系统的单点,这台服务器出 ...
- python 定时任务框架apscheduler
文章目录 安装 基本概念介绍 调度器的工作流程 实例1 -间隔性任务 实例2 - cron 任务 配置调度器 方法一 方法二 方法三: 启动调度器 方法一:使用默认的作业存储器: 方法二:使用数据库作 ...
- APScheduler(python 定时任务框架)最简单使用教程
有时候需要部署一些很简单的python定时任务,使用APScheduler是很好的选择.只需要简单的设置几个参数,就可以实现定时.定分甚至秒来跑. 第一步:用pip安装APScheduler pip ...
- [Dynamic Language] Python非子包引用
Python非子包引用 python的搜索路径其实是一个列表(sys.path) 导入模块时python会自动去找搜索这个列表当中的路径,如果路径中存在要导入的模块文件则导入成功. 在项目中如果要引用 ...
- [Python]定时任务框架 APScheduler
1.使用APScheduler教程 参考博客地址
随机推荐
- SQLAlchemy-介绍安装
一:概述 SQLAlchemy的SQL工具包和对象关系映射是一个全面的工具集,用来处理数据库和Python. 它有几个不同的功能领域,可以单独使用或组合使用. 所示的主要组件,组件依赖关系组织成层: ...
- wiki confluence安装
注意:安装前请先确认内存 至少2G 1.上传 atlassian-confluence-5.9.3-x64.bin 文件,修改权限 chmod 777 atlassian-confluence-5.9 ...
- opencv(0)安装与配置
1.windows下 1.1 exe安装 windows下可以安装opencv的exe版本,已经编译好了,很省事. 到https://opencv.org/releases.html下载需要的open ...
- (一)问候 HtmlUnit
第一节: HtmlUnit 简介 htmlunit 是一款开源的java 页面分析工具,读取页面后,可以有效的使用htmlunit分析页面上的内容.项目可以模拟浏览器运行,被誉为java浏览器的开源实 ...
- 使用Github的gh-pages分支展示一个页面
Github有一个Github pages的功能可以搭建博客或者托管网页,是免费使用的. 首先你的注册Github账号 下载安装git Github官网操作 登录到Github上,创建一个名为 Git ...
- CCF CSP 201512-2 消除类游戏
CCF计算机职业资格认证考试题解系列文章为meelo原创,请务必以链接形式注明本文地址 CCF CSP 201512-2 消除类游戏 问题描述 消除类游戏是深受大众欢迎的一种游戏,游戏在一个包含有n行 ...
- ViewPager中的子Activity无法响应OnActivityResult的解决方法
ViewPager子Activity通过startActivityForResult()跳转至OtherActivity,OtherActivity回传结果由ViewPager所在的父Activity ...
- linux虚拟机磁盘不够用以及进行扩容时遇到的问题
我使用的是:gparted live cd工具 系统是centOS6.2 使用gparted live cd工具进行无损分区,方法很简单,下载iso文件都在VMware对应的linux系统上设置CD ...
- nullptr
以前都是用0来表示空指针的,但由于0可以被隐式类型转换为整形,这就会存在一些问题.关键字nullptr是std::nullptr_t类型的值,用来指代空指针.nullptr和任何指针类型以及类成员指针 ...
- babel使用中不想使用 严格模式 如何去除?
使用babel进行es6转es5时,默认转化之后是严格模式,有些时候我们想去除严格模式. 解决方法如下 安装 babel-plugin-transform-remove-strict-mode 依赖 ...