python 定时任务
Python 定时任务
最近学习到了 python 中两种开启定时任务的方法,和大家分享一下心得。
- sched.scheduler()
- threading.Timer()
sched 定时任务
使用sched的套路如下:
s = sched.scheduler(time.time, time.sleep)
s.enter(delay, priority, func1, (arg1, arg2, ...))
s.enter(delay, priority, func2, (arg1, arg2, arg3, ...))
s.run()
第一步新建一个调度器;
第二步添加任务,可以添加多个任务;
第三步让调度器开始运行。
第二步各参数含义:
- delay 相对于调度器添加这个任务时刻的延时,以秒为单位;
- priority 优先级,数字越小优先级越高;
- func1 任务函数
- (arg1, arg2, ...) 任务函数的参数
import time
import sched
# 第一个工作函数
# 第二个参数 @starttime 表示任务开始的时间
# 很明显参数在建立这个任务的时候就已经传递过去了,至于任务何时开始执行就由调度器决定了
def worker(msg, starttime):
print u"任务执行的时刻", time.time(), "传达的消息是", msg, '任务建立时刻', starttime
# 创建一个调度器示例
# 第一参数是获取时间的函数,第二个参数是延时函数
print u'---------- 两个简单的例子 -------------'
print u'程序启动时刻:', time.time()
s = sched.scheduler(time.time, time.sleep)
s.enter(1, 1, worker, ('hello', time.time()))
s.enter(3, 1, worker, ('world', time.time()))
s.run() # 这一个 s.run() 启动上面的两个任务
print u'睡眠2秒前时刻:', time.time()
time.sleep(2)
print u'睡眠2秒结束时刻:', time.time()
# 重点关注下面2个任务,建立时间,启动时间
# 2个任务的建立时间都很好计算,但有没有发现 "hello world [3]" 的启动时间比建立时间晚 13 秒,
# 这不就是2个 sleep 的总延时吗?所以说启动时间并不一定就是 delay 能指定的,还需要看具体的程序环境,
# 如果程序堵塞的很厉害,已经浪费了一大段的时间还没有到 scheduler 能调度这个任务,当 scheduler 能调度这个
# 任务的时候,发现 delay 已经过去了, scheduler 为了弥补“罪过”,会马上启动这个任务。
# 任务 "hello world [15]" 就是一个很好的例子,正常情况下,程序没有阻塞的那么厉害,在scheduler 能调度这个任务的时候
# 发现 delay 还没到就等待,如果 delay 时间到了就可以在恰好指定的延时调用这个任务。
print u'\n\n---------- 两个复杂的例子 -------------'
s.enter(3, 1, worker, ('hello world [3]', time.time()))
print u'睡眠7秒前时刻:', time.time()
time.sleep(7)
print u'睡眠7秒结束时刻:', time.time()
s.enter(15, 1, worker, ('hello world [15]', time.time()))
print u'睡眠6秒前时刻:', time.time()
time.sleep(6)
print u'睡眠6秒结束时刻:', time.time()
s.run() # 过了2秒之后,启动另外一个任务
print u'程序结束时刻', time.time()
---------- 两个简单的例子 -------------
程序启动时刻: 1481731389.4
任务执行的时刻 1481731390.4 传达的消息是 hello 任务建立时刻 1481731389.4
任务执行的时刻 1481731392.41 传达的消息是 world 任务建立时刻 1481731389.4
睡眠2秒前时刻: 1481731392.41
睡眠2秒结束时刻: 1481731394.41
---------- 两个复杂的例子 -------------
睡眠7秒前时刻: 1481731394.41
睡眠7秒结束时刻: 1481731401.42
睡眠6秒前时刻: 1481731401.42
睡眠6秒结束时刻: 1481731407.42
任务执行的时刻 1481731407.42 传达的消息是 hello world [3] 任务建立时刻 1481731394.41
任务执行的时刻 1481731416.43 传达的消息是 hello world [15] 任务建立时刻 1481731401.42
程序结束时刻 1481731416.43
自调任务1
任务快结束时利用 scheduler 又重新调用自己让自己“活过来”。
# 计数器,一个循环任务,总共让自己执行3次
total = 0
# 第二个工作函数,自调任务,自己开启定时并启动。
def worker2(msg, starttime):
global total
total += 1
print u'当前时刻:', time.time(), '消息是:', msg, ' 启动时间是:', starttime
# 只要没有让自己调用到第3次,那么继续重头开始执行本任务
if total < 3:
# 这里的delay 可以重新指定
s.enter(5, 2, worker2, ('perfect world %d' % (total), time.time()))
s.run()
print u'程序开始时刻:', time.time()
# 开启自调任务
s.enter(5, 2, worker2, ('perfect world %d' % (total), time.time()))
s.run()
print u'程序结束时刻:', time.time()
程序开始时刻: 1481731439.42
当前时刻: 1481731444.43 消息是: perfect world 0 启动时间是: 1481731439.42
当前时刻: 1481731449.44 消息是: perfect world 1 启动时间是: 1481731444.43
当前时刻: 1481731454.44 消息是: perfect world 2 启动时间是: 1481731449.44
程序结束时刻: 1481731454.44
Threading.Timer() 定时任务
from threading import Timer
import time
def func(msg, starttime):
print u'程序启动时刻:', starttime, '当前时刻:', time.time(), '消息内容 --> %s' % (msg)
# 下面的两个语句和上面的 scheduler 效果一样的
Timer(5, func, ('hello', time.time())).start()
Timer(3, func, ('world', time.time())).start()
程序启动时刻: 1481731467.28 当前时刻: 1481731470.28 消息内容 --> world
程序启动时刻: 1481731467.28 当前时刻: 1481731472.28 消息内容 --> hello
循环任务2
利用 threading.Timer() 建立的自调任务
count = 0
def loopfunc(msg,starttime):
global count
print u'启动时刻:', starttime, ' 当前时刻:', time.time(), '消息 --> %s' % (msg)
count += 1
if count < 3:
Timer(3, loopfunc, ('world %d' % (count), time.time())).start()
Timer(3, loopfunc, ('world %d' % (count), time.time())).start()
启动时刻: 1481731476.35 当前时刻: 1481731479.35 消息 --> world 0
启动时刻: 1481731479.35 当前时刻: 1481731482.35 消息 --> world 1
启动时刻: 1481731482.35 当前时刻: 1481731485.35 消息 --> world 2
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定时任务
在项目中,我们可能遇到有定时任务的需求.其一:定时执行任务.例如每天早上 8 点定时推送早报.其二:每隔一个时间段就执行任务.比如:每隔一个小时提醒自己起来走动走动,避免长时间坐着.今天,我跟大家分享 ...
- [Dynamic Language] Python定时任务框架
APScheduler是一个Python定时任务框架,使用起来十分方便.提供了基于日期.固定时间间隔以及crontab类型的任务,并且可以持久化任务.并以daemon方式运行应用. 在APSchedu ...
- [转]Python定时任务框架APScheduler
APScheduler是基于Quartz的 一个Python定时任务框架,实现了Quartz的所有功能,使用起来十分方便.提供了基于日期.固定时间间隔以及crontab类型的任务,并且可以 持久化任务 ...
- Python 定时任务的实现方式
本文转载自: https://lz5z.com/Python%E5%AE%9A%E6%97%B6%E4%BB%BB%E5%8A%A1%E7%9A%84%E5%AE%9E%E7%8E%B0%E6%96% ...
- Python 定时任务框架 APScheduler 详解
APScheduler 最近想写个任务调度程序,于是研究了下 Python 中的任务调度工具,比较有名的是:Celery,RQ,APScheduler. Celery:非常强大的分布式任务调度框架 R ...
- python 定时任务APScheduler 使用介绍
python 定时任务APScheduler 使用介绍 介绍: APScheduler的全称是Advanced Python Scheduler.它是一个轻量级的 Python 定时任务调度框架. ...
- APScheduler(python 定时任务框架)最简单使用教程
有时候需要部署一些很简单的python定时任务,使用APScheduler是很好的选择.只需要简单的设置几个参数,就可以实现定时.定分甚至秒来跑. 第一步:用pip安装APScheduler pip ...
随机推荐
- 不可或缺 Windows Native (1) - C 语言: hello c
[源码下载] 不可或缺 Windows Native (1) - C 语言: hello c 作者:webabcd 介绍不可或缺 Windows Native 之 C 语言 在 Windows Sto ...
- 重新想象 Windows 8.1 Store Apps (92) - 其他新特性: CoreDispatcher, 日历, 自定义锁屏系列图片
[源码下载] 重新想象 Windows 8.1 Store Apps (92) - 其他新特性: CoreDispatcher, 日历, 自定义锁屏系列图片 作者:webabcd 介绍重新想象 Win ...
- USE “schema_name” in PostgreSQL
http://timmurphy.org/tag/mysql/ http://timmurphy.org/2009/11/17/use-schema_name-in-postgresql/ ===== ...
- 自己动手搞定支付宝手机网站支付接口 FOR ECShop
支付宝WAP网站版本的支付接口网上整合的比较少,看到很多网站在卖,顿觉无语. 主要是得自己查看支付宝官方提供的SDK中的开发文档. 支付宝sdk下载地址:https://doc.open.alipay ...
- gene框架文档 - 路由类 gene_router
路由类 Gene\Router 介绍 Gene\Router 是gene框架的核心类之一,本框架区别于其他常见框架的最大地方就是独特.强大.简单的路由定义等.路由强大灵活,支持回调.类方法:支持res ...
- LINUX重启MYSQL的命令
LINUX重启MYSQL的命令 标签: mysqllinuxservice脚本web服务server 2010-06-25 10:21 62152人阅读 评论(0) 收藏 举报 分类: Linux( ...
- 精简CSS代码
精简CSS代码可以帮助减小样式文件的大小,使代码清晰,方便维护. 使用简写属性及默认值 .header { margin-top: 10px; margin-right: 20px; margin-b ...
- ABAP程序中退出操作(CHECK, EXIT, RETURN, LEAVE PROGRAM)
这里总结一下几个常用的退出操作: CHECK.(SAP官方推荐只在循环中使用) 1)CHECK 后面要跟一个表达式,当表达式值为假(false)时,CHECK发生作用,退出循环(LOOP)或处理程序 ...
- English Training Material - 03
Cross-cultural understanding (2) 1 The following text is about cultural diversity. Read it through o ...
- JSP--JavaBean
JSP 最强有力的一个方面就是能够使用 JavaBean 组件. 按照 Sun 公司的定义, JavaBean是一个可重复使用的软件组件.实际上 JavaBean 是一种 Java 类,通过封装属性和 ...