python定时任务使用方法如下:

  1. import sched
  2. shelder = sched.scheduler(time.time, time.sleep)
  3. shelder.enter(2, 0, print_time, ())
  4. shelder.run()

执行顺序说明如下:

1、创建一个定时任务执行的实例
2、将定时任务插入到执行队列中
3、启动定时任务
enter方法的参数说明:
第一个参数是在任务启动多少秒后执行
第二个参数是任务优先级
第三个参数是要执行的方法
第四个参数是方法要传进去的参数,没有的话直接使用()
 
实例1:顺序执行
  1. import time
  2. import sched
  3. def print_time():
  4. print 'now the time is:',time.time()
  5. def do_some_times():
  6. print 'begin time:',time.time()
  7. shelder.enter(2, 0, print_time, ())
  8. shelder.enter(2, 0, print_time, ())
  9. shelder.enter(5, 1, print_time, ())
  10. shelder.run()
  11. print 'end time:',time.time()
  12. do_some_times()

运行结果:

begin time: 1525682354.85

now the time is: 1525682356.86

now the time is: 1525682356.86

now the time is: 1525682359.86

end time: 1525682359.86

这里后面的时间都是一样的是因为表示的是加入到队列时间

在涉及到多线程的问题时使用上面的方法就会引入线程安全的限制,官方手册上也做了充分的说明,具体如下:

In multi-threaded environments, the scheduler class has limitations with respect to thread-safety, inability to insert a new task before the one currently pending in a running scheduler, and holding up the main thread until the event queue is empty. Instead, the preferred approach is to use the threading.Timer class instead.
最终的意思就是使用threading的Timer进行替代
 
实例2:线程安全的
  1. from threading import Timer
  2. import time
  3. def print_time():
  4. print 'now the time is:',time.time()
  5. def print_times():
  6. print 'begin time is:',time.time()
  7. Timer(5, print_time,(())).start()
  8. Timer(10, print_time,(())).start()
  9. time.sleep(2)
  10. print 'end time is:',time.time()
  11.  
  12. print_times()

运行结果:

begin time is: 1525682440.55

end time is: 1525682442.55

now the time is: 1525682445.55

now the time is: 1525682450.55

实例3:任务自调度

  1. from threading import Timer
  2. import time
  3.  
  4. counttimes=3
  5. def print_time():
  6. global counttimes
  7. if counttimes > 0:
  8. print 'now the time is %d,%f:' % (counttimes,time.time())
  9. Timer(5, print_time,(())).start()
  10. counttimes -= 1
  11.  
  12. def print_times():
  13. print 'begin time is:',time.time()
  14. Timer(5, print_time,(())).start()
  15. time.sleep(2)
  16. print 'end time is:',time.time()
  17. print_times()

运行结果:

begin time is: 1525682594.3

end time is: 1525682596.3

now the time is 3,1525682599.300889:

now the time is 2,1525682604.302403:

now the time is 1,1525682609.302912:

附录

常用schelder方法:

scheduler.enterabs(time, priority, action, argument)
scheduler.enter(delay, priority, action, argument)
scheduler.cancel(event)
删除一个任务事件
scheduler.empty()
任务队列是否为空
scheduler.run()
启动任务,执行下一个任务时会进行等待
scheduler.queue
任务队列
参考文档:
 

python 定时服务模块的更多相关文章

  1. 第九章 Net 5.0 快速开发框架 YC.Boilerplate --定时服务 Quartz.net

    在线文档:http://doc.yc-l.com/#/README 在线演示地址:http://yc.yc-l.com/#/login 源码github:https://github.com/linb ...

  2. Python循环定时服务功能(相似contrab)

    Python实现的循环定时服务功能.类似于Linux下的contrab功能.主要通过定时器实现. 注:Python中的threading.timer是基于线程实现的.每次定时事件产生时.回调完响应函数 ...

  3. python定时利用QQ邮件发送天气预报

    大致介绍 好久没有写博客了,正好今天有时间把前几天写的利用python定时发送QQ邮件记录一下 1.首先利用request库去请求数据,天气预报使用的是和风天气的API(www.heweather.c ...

  4. 用python定时文章发布wordpress

    用python定时文章发布wordpress: 流程: 采集 - 筛选文章 - wordpress文章发布. wordpress文章发布代码:python利用模块xmlrpclib发布文章非常便捷,省 ...

  5. Python队列服务 Python RQ Functions from the __main__ module cannot be processed by workers.

    在使用Python队列服务 Python RQ 时候的报错: Functions from the __main__ module cannot be processed by workers. 原因 ...

  6. sae python安装第三方模块

    sae python安装第三方模块 经过这一个星期的折腾,发现编程真心不是看出来的,真心是跟着书上的代码敲出来的.sae的服务做得很好,不过有时候会崩就是了.当sae上没有自己所需要的第三方模块时,可 ...

  7. yum安装memcache,mongo扩展以及python的mysql模块安装

    //启动memcached/usr/local/memcached/bin/memcached -d -c 10240 -m 1024 -p 11211 -u root/usr/local/memca ...

  8. python的pika模块操作rabbitmq

    上一篇博文rabbitmq的构架和原理,了解了rabbitmq的使用原理,接下来使用python的pika模块实现使用rabbitmq. 环境搭建 安装python,不会的请参考Linux安装配置py ...

  9. 写一个python的服务监控程序

    写一个python的服务监控程序 前言: Redhat下安装Python2.7 rhel6.4自带的是2.6, 发现有的机器是python2.4. 到python网站下载源代码,解压到Redhat上, ...

随机推荐

  1. android sdk里的各目录作用

    android sdk里的各目录作用 AVD Manager.exe:虚拟机管理工具,用于建立和管理虚拟机. SDK Manager.exe:sdk管理工具,用于管理.下载sdk.sdk工具,能及扩展 ...

  2. Tapestry: Obtained resource by @Inject is NULL

    Issue: When you inject some resources by @Inject Annotation in Tapestry Page or other components, yo ...

  3. ADO.NET之Parameter属性

    在ADO.NET中, public SqlParameterCollection Parameters {get;}会得到一个SqlParameter属性.下面通过一个例子进行详细的认识吧. //例如 ...

  4. ROS_Kinetic_18 使用V-Rep3.3.1和Matlab2015b(vrep_ros_bridge)续

    ROS_Kinetic_18 使用V-Rep3.3.1和Matlab2015b(vrep_ros_bridge)续 上一节配置的v-rep在ros kinetic中是可以看图像,并订阅主题的,但是无法 ...

  5. 【leetcode79】Single Number III

    题目描述: 给定一个数组,里面只有两个数组,只是出现一次,其余的数字都是出现两次,找出这个两个数字,数组形式输出 原文描述: Given an array of numbers nums, in wh ...

  6. 7、Libgdx网络操作

    (官网:www.libgdx.cn) Libgdx包含了一些跨平台的网络操作类,这些类在Gdx.net中. 特性 跨平台HTTP请求 多平台TCP C/S Socket支持(可配置) TCP C/S优 ...

  7. 【一天一道LeetCode】#41. First Missing Positive

    一天一道LeetCode系列 (一)题目 Given an unsorted integer array, find the first missing positive integer. For e ...

  8. Mahout朴素贝叶斯文本分类

    Mahout朴素贝叶斯文本分类算法 Mahout贝叶斯分类器按照官方的说法,是按照<Tackling the PoorAssumptions of Naive Bayes Text Classi ...

  9. FFMPEG结构体分析:AVCodec

    注:写了一系列的结构体的分析的文章,在这里列一个列表: FFMPEG结构体分析:AVFrame FFMPEG结构体分析:AVFormatContext FFMPEG结构体分析:AVCodecConte ...

  10. DBUtils源码分析

    其实,在这篇文章里,我只是分析了dbutis的query的运作流程. 至于类为什么要这样设计,蕴含的设计模式等等高级知识点咱们在下节再探讨. 先看看最简单的DBUtils是如何工作的. 数据库里有一张 ...