Python中APScheduler模块的使用
2016-01-26 16:41
513 查看
APScheduler介绍
Advanced Python Scheduler (APScheduler) is a Python library that lets you schedule your Python code to be executed later, either just once or periodically. You can add new jobs or remove old ones on the fly as you please. If you store your jobs in a database,they will also survive scheduler restarts and maintain their state. When the scheduler is restarted, it will then run all the jobs it should have run while it was offline.
个人翻译:
APScheduler是一个Python库,它能够让你安排你的Python代码延后执行、执行一次或者定期执行。你可以随意添加新的任务或删除旧的任务。如果用数据库存储任务,调度程序重启期间会维护任务的状态。当调度程序重启后,将运行所有本应在掉线时执行的任务。
代码
可以采用两种方式添加任务,调用add_job()方法或使用scheduled_job()装饰器。调用add_job方法:
import datetime from apscheduler.schedulers.blocking import BlockingScheduler scheduler = BlockingScheduler() def test(): print "now is '%s' " % datetime.datetime.now() scheduler.add_job(test, "cron", second="*/3") try: scheduler.start() except (KeyboardInterrupt, SystemExit): scheduler.shutdown()
使用装饰器:
import datetime from apscheduler.schedulers.blocking import BlockingScheduler scheduler = BlockingScheduler() @scheduler.scheduled_job("cron", second="*/3") def test(): print "now is '%s' " % datetime.datetime.now() try: scheduler.start() except (KeyboardInterrupt, SystemExit): scheduler.shutdown(
cron表达式说明
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 |
参考资料
# 官方文档 apschedule_docs = "http://apscheduler.readthedocs.org/en/latest/index.html" # Python定时任务框架APScheduler 3.0.3 Cron示例 blog1 = "http://www.cnblogs.com/leleroyn/p/4501359.html"
相关文章推荐
- python正则表达式例子说明
- 【LEETCODE】313-Super Ugly Number [Python]
- python-->>第四天
- python模块系列之 - time,datetime,calendar
- Python之 with and as
- Python3.4 tkinter,PIL图片转换(GUI)
- Python3.4 垃圾清理(GUI)
- python代码片段——获得三天前时间
- python正则--简单匹配身份证号
- Python函数中*args和**kwargs来传递变长参数的用法
- python 之 readline
- 文本预处理小结
- Python之I/O
- python进阶十_正則表達式(一)
- 决策树
- 朴素贝叶斯
- python 安装模块
- Python入门——A Byte of Python
- python全局变量
- Pillow-PIL的完全Fok替代库--完美解决报错The _imagingft C module is not installed的解决方案