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 ...
随机推荐
- SqL数据库发布订阅非聚集索引没有被复制到订阅服务器的解决方案
Non-Clustered Indexes not copying in Transactional Replication : SQL Server 2008 方法1: You have trans ...
- jquery实现表格中点击相应行变色功能
对于一个表格,为了使我们选中的项更容易区分,需要为选中项添加高亮,同时也需要,将其他项的高亮形式去除.类似于: <!DOCTYPE html> <html lang="en ...
- jQuery中常用的元素查找方法
$("#myELement") 选择id值等于myElement的元素,id值不能重复在文档中只能有一个id值是myElement所以得到的是唯一的元素 $("div&q ...
- SqlServer定时备份数据库和定时杀死数据库死锁解决
上周五组长对我说了一句要杀死数据库的死锁进程,有时候同一时刻不停写入数据库会造成这种情况的发生,因为自己对数据库不是很熟悉,突然组长说了我也就决定一定要倒腾一下,不然自己怎么提高呢?现在不研究,说不定 ...
- unity3d拓展编辑器Editor的使用
Editor可以拓展Inspector窗口 可以通过代码自己绘制监测面板 先来看一个效果:
- python函数和常用模块(一),Day3
set集合 函数 三元运算 lambda表达式 内置函数1 文件操作 set集合 创建 se = {"123", "456"} # 直接创建一个集合 se = ...
- Python基础(一),Day1
python的安装 python2.x与3.x的部分区别 第一个python程序 变量 字符编码 注释 格式化字符串 用户输入 常用的模块初始 if判断 循环语句 作业 1.python的安装 可以在 ...
- (HY000): Cannot modify @@session.sql_log_bin inside a transaction
昨天,线上发生一例(HY000): Cannot modify @@session.sql_log_bin inside a transaction代码缺少显示的start transaction控制 ...
- android sdk无法更新或者更新缓慢的解决方案
win7安装android sdk老出 Fetching https://dl-ssl.google.com/android/repository/addon .这是android sdk不能连接到谷 ...
- SAP和Java系统的Webservice实例
简介: 关于Webservice的概念和原理,简单来讲,Webservice是一种基于SOAP传输协议,用WSDL描述,用XML封装数据的接口技术.由于其跨平台.跨防火墙.开发成本低.开发周期短等优势 ...