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 定时任务的更多相关文章

  1. Python定时任务框架APScheduler 3.0.3 Cron示例

    APScheduler是基于Quartz的一个Python定时任务框架,实现了Quartz的所有功能,使用起来十分方便.提供了基于日期.固定时间间隔以及crontab类型的任务,并且可以持久化任务.基 ...

  2. Python定时任务框架APScheduler

    http://blog.csdn.net/chosen0ne/article/details/7842421 APScheduler是基于Quartz的一个Python定时任务框架,实现了Quartz ...

  3. Python定时任务

    在项目中,我们可能遇到有定时任务的需求.其一:定时执行任务.例如每天早上 8 点定时推送早报.其二:每隔一个时间段就执行任务.比如:每隔一个小时提醒自己起来走动走动,避免长时间坐着.今天,我跟大家分享 ...

  4. [Dynamic Language] Python定时任务框架

    APScheduler是一个Python定时任务框架,使用起来十分方便.提供了基于日期.固定时间间隔以及crontab类型的任务,并且可以持久化任务.并以daemon方式运行应用. 在APSchedu ...

  5. [转]Python定时任务框架APScheduler

    APScheduler是基于Quartz的 一个Python定时任务框架,实现了Quartz的所有功能,使用起来十分方便.提供了基于日期.固定时间间隔以及crontab类型的任务,并且可以 持久化任务 ...

  6. 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% ...

  7. Python 定时任务框架 APScheduler 详解

    APScheduler 最近想写个任务调度程序,于是研究了下 Python 中的任务调度工具,比较有名的是:Celery,RQ,APScheduler. Celery:非常强大的分布式任务调度框架 R ...

  8. python 定时任务APScheduler 使用介绍

    python 定时任务APScheduler 使用介绍   介绍: APScheduler的全称是Advanced Python Scheduler.它是一个轻量级的 Python 定时任务调度框架. ...

  9. APScheduler(python 定时任务框架)最简单使用教程

    有时候需要部署一些很简单的python定时任务,使用APScheduler是很好的选择.只需要简单的设置几个参数,就可以实现定时.定分甚至秒来跑. 第一步:用pip安装APScheduler pip ...

随机推荐

  1. ASP.NET MVC使用动态产生meta

    在ASP.NET中,我们是很容易动态为header节点添加meta信息.<动态修改网页Header属性,Title,Meta标签等>http://www.cnblogs.com/insus ...

  2. appt查看apk信息

    aapt dump badging app-debug.apk

  3. AEAI BPM流程集成平台V3.0.2版本开源发布

    本次开源发布的是AEAI BPMV3.0.2版流程平台,该版本是数通畅联首次正式对外发布的版本,产品现已开源并上传至开源社区http://www.oschina.net/p/aeai-bpm. 产品说 ...

  4. windows的host文件的位置和作用

    在Window系统中有个Hosts文件(没有后缀名),在Windows98系统下该文件在Windows目录,在Windows2000/XP系统中位于C:\Winnt\System32\Drivers\ ...

  5. 创建一个弹出DIV窗口

    创建一个弹出DIV窗口 摘自:   http://www.cnblogs.com/TivonStone/archive/2012/03/20/2407919.html 创建一个弹出DIV窗口可能是现在 ...

  6. 【C#进阶系列】04 类型基础

    关于System.Object 所有类型都从System.Object派生而来. System.Object的公共方法中ToString()一般是返回对象的类型的全名,只有Int32这些类型将其重写后 ...

  7. csharp: MongoDB

    安装配置: Install MongoDB on Windows(安装配置官方参考) http://docs.mongodb.org/manual/tutorial/install-mongodb-o ...

  8. css样式reset

    初始化样式. @CHARSET "UTF-8"; body,ur,ol,li,p,h1,h2,h3,h4,h5,h6,form,fieldset,table,td,img,div, ...

  9. ztree树 叶子节点路径的集合

    1.Question Description: ztree树各个节点都带有路径,如“/根节点”,"/根节点/一级节点",“根节点/一级节点/二级节点‘; 现在想获取所选的最末级节点 ...

  10. NYOJ 21 三个水杯

    三个水杯 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 给出三个水杯,大小不一,并且只有最大的水杯的水是装满的,其余两个为空杯子.三个水杯之间相互倒水,并且水杯没有 ...