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

(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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值