twisted 的中有一个定时执行类,很象AS3 中的 timer类
LoopingCall 有个start方法,
start方法
第一个参数是函数执行间隔;
第二参数是否现在马上执行;
注意:被调的函数需要有一个self的传参;
下面是在我newjhService对LoopingCall的应用范例:
- class newjhService(object):
- def __init__(self, pool):
- self.conn_pool = pool
- LoopingCall(self._keepAlive).start(36000, False)
- def _keepAlive(self):
- print 'Running Keep Alive...'
- self.conn_pool.runOperation('SELECT 1')
- def checkUser(self, username, password):
- """
- Using authentication
- """
- print 'username : %s passwrd : %s' % (username, password)
- def cb(rs):
- print rs
- if rs == () :
- return False
- #ret = [ObjectProxy(build_user(row)) for row in rs]
- #print ret
- #return ArrayCollection(ret)
- #if ret == []:
- #return False
- return True
- def eb(failure):
- # TODO nick: logging
- return ArrayCollection()
- d = self.conn_pool.runQuery("SELECT * FROM user where " + \
- "UserName= '" +username + "' and Password = '" + \
- password +"'" ).addErrback(eb).addCallback(cb)
- return d
阅读(2162) | 评论(0) | 转发(0) |