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上, ...
随机推荐
- SDL2源代码分析7:显示(SDL_RenderPresent())
===================================================== SDL源代码分析系列文章列表: SDL2源代码分析1:初始化(SDL_Init()) SDL ...
- Cocos2D:塔防游戏制作之旅(十八)
在Enemy.m的getDamaged:方法只给你添加如下1行(在if条件内): [theGame awardGold:200]; 现在运行游戏你将注意到你不能放置超出你资源金币的炮塔了.当然杀死敌人 ...
- django练习——博客系统优化
一直准备使用Django搭建一个个人网站,最近终于开始动手,上周已经完成了基本博客功能的搭建(http://blog.csdn.net/hcx25909/article/details/2460133 ...
- iOS中 UIProgressView 技术分享
UIProgressView 继承自UIView,用来显示进度的,如音乐,视频的缓冲进度,文件的上传下载进度等.让用户知道当前操作完成了多少,离操作结束还有多远 AppDelegate.m Progr ...
- Maven nexus安装、配置和使用
简介 Nexus 可以代理并缓存 Maven 构件,当 Maven 需要下载构件的时候,就不需要反复的请求中央仓库. 有些公司都不提供外网给项目组人员,因此就不能使用 Maven 访问 ...
- BLOCK/字面量(语法糖)OC——第六天
1.//block ,块语法,实质是匿名函数,是对C语言中函数的扩充,扩展: //block 语法可以用来保存一段代码或者用来调用一段封装好的代码: //block 语法由于是C语言实现的,所以执 ...
- 【Unity Shaders】Using Textures for Effects介绍
本系列主要参考<Unity Shaders and Effects Cookbook>一书(感谢原书作者),同时会加上一点个人理解或拓展. 这里是本书所有的插图.这里是本书所需的代码和资源 ...
- SpriteBuilder中节点位置类型为百分比时不能定位的解决
Ball.ccb类型是Node,其中有个子节点为Color Node,其中物理使能. MainScene.ccb中加入一个物理节点,将Ball.ccb拖入其中,成为该物理节点的孩子,这时出现了一个&q ...
- Django访问量和页面点击数统计
http://blog.csdn.net/pipisorry/article/details/47396311 下面是在模板中做一个简单的页面点击数统计.model阅读量统计.用户访问量统计的方法 简 ...
- studio多渠道打包
由于国内Android市场众多渠道,为了统计每个渠道的下载及其它数据统计,就需要我们针对每个渠道单独打包,如果让你打几十个市场的包岂不烦死了,不过有了Gradle,这事就简单了. 友盟多渠道打包 废话 ...