python 定时服务模块
python定时任务使用方法如下:
import sched
shelder = sched.scheduler(time.time, time.sleep)
shelder.enter(2, 0, print_time, ())
shelder.run()
执行顺序说明如下:
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
这里后面的时间都是一样的是因为表示的是加入到队列时间
在涉及到多线程的问题时使用上面的方法就会引入线程安全的限制,官方手册上也做了充分的说明,具体如下:
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方法:
python 定时服务模块的更多相关文章
- 第九章 Net 5.0 快速开发框架 YC.Boilerplate --定时服务 Quartz.net
在线文档:http://doc.yc-l.com/#/README 在线演示地址:http://yc.yc-l.com/#/login 源码github:https://github.com/linb ...
- Python循环定时服务功能(相似contrab)
Python实现的循环定时服务功能.类似于Linux下的contrab功能.主要通过定时器实现. 注:Python中的threading.timer是基于线程实现的.每次定时事件产生时.回调完响应函数 ...
- python定时利用QQ邮件发送天气预报
大致介绍 好久没有写博客了,正好今天有时间把前几天写的利用python定时发送QQ邮件记录一下 1.首先利用request库去请求数据,天气预报使用的是和风天气的API(www.heweather.c ...
- 用python定时文章发布wordpress
用python定时文章发布wordpress: 流程: 采集 - 筛选文章 - wordpress文章发布. wordpress文章发布代码:python利用模块xmlrpclib发布文章非常便捷,省 ...
- 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. 原因 ...
- sae python安装第三方模块
sae python安装第三方模块 经过这一个星期的折腾,发现编程真心不是看出来的,真心是跟着书上的代码敲出来的.sae的服务做得很好,不过有时候会崩就是了.当sae上没有自己所需要的第三方模块时,可 ...
- yum安装memcache,mongo扩展以及python的mysql模块安装
//启动memcached/usr/local/memcached/bin/memcached -d -c 10240 -m 1024 -p 11211 -u root/usr/local/memca ...
- python的pika模块操作rabbitmq
上一篇博文rabbitmq的构架和原理,了解了rabbitmq的使用原理,接下来使用python的pika模块实现使用rabbitmq. 环境搭建 安装python,不会的请参考Linux安装配置py ...
- 写一个python的服务监控程序
写一个python的服务监控程序 前言: Redhat下安装Python2.7 rhel6.4自带的是2.6, 发现有的机器是python2.4. 到python网站下载源代码,解压到Redhat上, ...
随机推荐
- 怎么避免在类实现的cpp文件中不要多次进行类声明
方法1: 类的头文件中写 #pragma once class MyTeacher { public: MyTeacher(); ~MyTeacher(); }; 方法2: 类的头文件中写 #ifnd ...
- svn 集成 redmine 账户验证的终极解决方案
svn 集成 redmine 账户验证的终极解决方案 赖勇浩(http://laiyonghao.com) 动机 对于大部分开发团队来说,一般都需要一套 SCM 系统,通常是 svn + redmin ...
- 使用UE4/Unity创建VR项目
一.主要的步骤是说一下使用UE4,在此之前先说一下使用unity创建的VR项目 1.unity创建oculus rift dk2项目 在unity中创建一个简单的场景,让摄像机能看见场景中的物体,不对 ...
- matlab中 mcc/mbuild/mex 区别
mcc 的作用是将 .m文件编译为 c/c++动态链接库文件,使你可以在 c/c++程序中使用 matlab的一些函数功能.mcc 也可以将.m文件编译为exe可执行文件. mex 的作用是将 c/c ...
- SharePoint 2013: The "New Web Application" button is disabled is the central administration
安装完sharepoint foundation2013后,直接通过url访问管理中心,想进application management去新建的话会发现新建按钮是灰色的,即使你用的是系统管理员账号并且 ...
- GROUP BY 的实现与优化
由于GROUP BY实际上也同样需要进行排序操作,而且与ORDER BY相比,GROUP BY主要只是多了排序之后的分组操作.当然,如果在分组的时候还使用了其他的一些聚合函数,那么还需要一些聚合函数的 ...
- Cocos2D旋转炮塔到指定角度(三)
到目前为止都很美好! 但是却有一点奇怪,因为炮塔一下子跳转到指定位置去射击,并不是平滑的跟随触摸去转动到指定位置.你可以修复这个问题,但是这需要略微一点的重构(refactoring). 首先打开He ...
- Socket编程实践(8) --Select-I/O复用
五种I/O模型介绍 (1)阻塞I/O[默认] 当上层应用App调用recv系统调用时,如果对等方没有发送数据(Linux内核缓冲区中没有数据),上层应用Application1将阻塞;当对等方发送了数 ...
- [Django高级]理解django中的中间件机制和执行顺序
原文来自 Understanding Django Middlewares, 这篇文章从整体上介绍了django中中间件定义,作用,和怎么样自己写中间件 –orangleliu. 注:middlewa ...
- (NO.00001)iOS游戏SpeedBoy Lite成形记(七)
因为我们之前在GameScene中建立的2个数组,分别为player和label的数组.大家可以注意到其中每个元素是一一对应的. 知道了这层关系,我们尝试来更新matchRun方法: CCAction ...