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

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

执行顺序说明如下:

1、创建一个定时任务执行的实例
2、将定时任务插入到执行队列中
3、启动定时任务
enter方法的参数说明:
第一个参数是在任务启动多少秒后执行
第二个参数是任务优先级
第三个参数是要执行的方法
第四个参数是方法要传进去的参数,没有的话直接使用()
 
实例1:顺序执行
import time
import sched
def print_time():
print 'now the time is:',time.time()
def do_some_times():
print 'begin time:',time.time()
shelder.enter(2, 0, print_time, ())
shelder.enter(2, 0, print_time, ())
shelder.enter(5, 1, print_time, ())
shelder.run()
print 'end time:',time.time()
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:线程安全的
from threading import Timer
import time
def print_time():
print 'now the time is:',time.time()
def print_times():
print 'begin time is:',time.time()
Timer(5, print_time,(())).start()
Timer(10, print_time,(())).start()
time.sleep(2)
print 'end time is:',time.time() 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:任务自调度

from threading import Timer
import time counttimes=3
def print_time():
global counttimes
if counttimes > 0:
print 'now the time is %d,%f:' % (counttimes,time.time())
Timer(5, print_time,(())).start()
counttimes -= 1 def print_times():
print 'begin time is:',time.time()
Timer(5, print_time,(())).start()
time.sleep(2)
print 'end time is:',time.time()
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. JSP实现界面的自动跳转的几种方式

    下面来谈一谈在jsp中实现的几种界面自动跳转的方法. 使用JavaScript脚本 <html> <script language=javascript> function o ...

  2. HttpClient 解决中文乱码

    public static String httpGet(String url) { try { HttpGet httpGet = new HttpGet(url); HttpClient clie ...

  3. 海量数据挖掘MMDS week7: 相似项的发现:面向高相似度的方法

    http://blog.csdn.net/pipisorry/article/details/49742907 海量数据挖掘Mining Massive Datasets(MMDs) -Jure Le ...

  4. 关于Class文件

    什么是Class文件 Java人对class文件肯定很熟悉了,它是Java源码编译后的产物.JVM运行时负责加载class文件,并根据class定义的执行逻辑运行.java为了将硬件底层的差异屏蔽掉, ...

  5. React 语法之let和const命令

    let命令 基本用法 ES6新增了let命令,用来声明变量.它的用法类似于var,但是所声明的变量,只在let命令所在的代码块内有效. { let a = 10; var b = 1; } a // ...

  6. Volley请求

    1. Volley简介 我们平时在开发Android应用的时候不可避免地都需要用到网络技术,而多数情况下应用程序都会使用HTTP协议来发送和接收网络数据.Android系统中主要提供了两种方式来进行H ...

  7. 9.8、Libgdx的返回键和菜单键捕获

    (官网:www.libgdx.cn) 当用户在Android设备中点击返回键是,通常关闭当前运行的activity.游戏可能会给出一个确认对话框让用户选择退出或继续.要这样的话需要捕获返回键: Gdx ...

  8. Linux Shell 命令--rename

    重命名文件,经常用到mv命令,批量重命名文件rename是最好的选择,Linux的rename 命令有两个版本,一个是C语言版本的,一个是Perl语言版本的,判断方法:输入man rename 看到第 ...

  9. 关于jQuery中的trigger和triggerHandler方法的使用

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. StarUML配置Word生成文档模板

    来源:fasiondog 许多UML建模工具可以自动生成文档,让需求人员.开发人员专心于需求.设计的建模.当然为了能够生成符合自己要求的模板,需对建模时的目录结构(模型和包)有所规划和要求,否则很难生 ...