TimerService是twisted里提供的一个对loopingcall的service封装。
是在服务运行的期间反复执行某个方法的手段,文档里没有,其实也很简单。
- #! /usr/bin/python
- # -*- coding: utf-8 -*-
- '''
- Created on May 5, 2009
- @author: Daniel
- '''
- from twisted.application import service, internet
- from twisted.application.internet import TimerService
- from twisted.python import log
- import sys
- #log.startLogging(sys.stdout)
- #log.startLogging(open('./log.txt', 'w'))
- application = service.Application('RSSServer')
- def test():
- #log.msg('looping')
- print 'looping'
- ts = TimerService(1, test)
- ts.setServiceParent(application)
现在twistd -noy 一下,应该看到服务正常循环调用。换个log方式,以twisted log的方法。
- #! /usr/bin/python
- # -*- coding: utf-8 -*-
- '''
- Created on May 5, 2009
- @author: Daniel
- '''
- from twisted.application import service, internet
- from twisted.application.internet import TimerService
- from twisted.python import log
- import sys
- log.startLogging(sys.stdout)
- #log.startLogging(open('./log.txt', 'w'))
- application = service.Application('RSSServer')
- def test():
- log.msg('looping')
- #print 'looping'
- ts = TimerService(1, test)
- ts.setServiceParent(application)
运行一下,试一试。呵呵。
阅读(1773) | 评论(0) | 转发(0) |