Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5018934
  • 博文数量: 921
  • 博客积分: 16037
  • 博客等级: 上将
  • 技术积分: 8469
  • 用 户 组: 普通用户
  • 注册时间: 2006-04-05 02:08
文章分类

全部博文(921)

文章存档

2020年(1)

2019年(3)

2018年(3)

2017年(6)

2016年(47)

2015年(72)

2014年(25)

2013年(72)

2012年(125)

2011年(182)

2010年(42)

2009年(14)

2008年(85)

2007年(89)

2006年(155)

分类: Python/Ruby

2012-04-06 10:04:41

TimerService是twisted里提供的一个对loopingcall的service封装。

是在服务运行的期间反复执行某个方法的手段,文档里没有,其实也很简单。

点击(此处)折叠或打开

  1. #! /usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. '''
  4. Created on May 5, 2009
  5. @author: Daniel
  6. '''
  7. from twisted.application import service, internet
  8. from twisted.application.internet import TimerService
  9. from twisted.python import log
  10. import sys
  11. #log.startLogging(sys.stdout)
  12. #log.startLogging(open('./log.txt', 'w'))
  13. application = service.Application('RSSServer')
  14. def test():
  15.     #log.msg('looping')
  16.     print 'looping'
  17. ts = TimerService(1, test)
  18. ts.setServiceParent(application)

现在twistd -noy 一下,应该看到服务正常循环调用。换个log方式,以twisted log的方法。

  1. #! /usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. '''
  4. Created on May 5, 2009
  5. @author: Daniel
  6. '''
  7. from twisted.application import service, internet
  8. from twisted.application.internet import TimerService
  9. from twisted.python import log
  10. import sys
  11. log.startLogging(sys.stdout)
  12. #log.startLogging(open('./log.txt', 'w'))

  13. application = service.Application('RSSServer')
  14. def test():
  15.     log.msg('looping')
  16.     #print 'looping'
  17. ts = TimerService(1, test)
  18. ts.setServiceParent(application)

运行一下,试一试。呵呵。


 

阅读(1741) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~