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. Windows Azure开发者任务之五:配置虚拟机的“规模”

    指定虚拟机的“规模”是怎么一回事? 我们可以指定角色将要部署于其上的虚拟机的“规模”.虚拟机的“规模”是指: 1,CPU核心数 2,内存容量 3,本地文件系统的体积 我们可以针对具体的角色来指定虚拟机 ...

  2. Python入门笔记(13):列表解析

    一.列表解析 列表解析来自函数式编程语言(haskell),语法如下: [expr for iter_var in iterable] [expr for iter_var in iterable i ...

  3. vs2012中编译时出现程序集所使用的版本高于所引用的版本

    我在运行别人的项目时出现版本不兼容的问题:

  4. [moka同学笔记]yii2.0的下拉菜单与bootstrap下拉菜单

    1.yii2下拉菜单 <li class="dropdown"><a href="#" class="dropdown-toggle ...

  5. Java的集合框架

    01.为什么要使用集合框架? 解析:如果并不知道程序运行时会需要多少对象,或者需要更复杂方式存储对象,那么可以使用Java集合框架. 如果启用集合的删除方法,那么集合中所有元素的索引会自动维护. 集合 ...

  6. jquery 下拉选择框/复选框常用操作

    通常 1.我们需要获取select中选中的值,可以使用: $("#selectID").find("option:selected").val();  --一般 ...

  7. task mysqld:26208 blocked for more than 120 seconds

    早上10点左右,某台线上ECS服务器突然没响应. 查看日志,发现如下信息: Aug 14 03:26:01 localhost rsyslogd: [origin software="rsy ...

  8. AJAX请求中含有数组解决办法

    当我们发送AJAX请求的数据中带有数组时,是不能像普通JSON数据一样,直接放在data里发送给后台,比如有这样一个数据需要发送给后台: { "orderId": 22, &quo ...

  9. 安卓开发-See the log file\.metadata\.log.

    今天在给安卓项目res-valus-string.xml 中字符串修改的时候,突然eclipse卡住了 然后任务管理器关掉之后,重新打开 显示一个错误 百度了一下 eclipse启动报错,让查看.me ...

  10. git proxy

    git config --global http.proxy http://127.0.0.1:1080 git config --global https.proxy https://127.0.0 ...