您的位置:首页 > 编程语言 > Python开发

gevent中的定时器 timer的简单使用

2014-02-09 23:06 2667 查看
(1)timer在gevent的core模块,core模块是对 libev库的包装,源码在:https://github.com/surfly/gevent/blob/master/gevent/core.ppyx

(2)我们可以用类loop(反应器)的timer的方法获取一个timer实例,具体函数签名如下:

def timer(self, double after, double repeat=0.0, ref=True, priority=None):
return timer(self, after, repeat, ref, priority)

参数意义:

after--after秒后,第一次运行注册在此timer的回调函数(通过timer的start方法)。当为0,会马上调用回调函数,然后repeat秒后再调用回调函数。

repeat--反复运行注册的回调函数,间隔为repeat秒。默认值为0.0,即调用一次回调函数。当repeat<为正数,会持续每repeat秒回调,除非我们调用timer的stop方法。

(3)实例代码:

import gevent
def test(a,b):
print a,b
loop = gevent.get_hub().loop
t =loop.timer(0.0, 5) # timer注册回调函数后马上调用回调函数,然后反复每隔5秒调用回调函数
timer.start(test,1,2)
loop.run() #运行反应器loop
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息