Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5096664
  • 博文数量: 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:24:03

is pretty cool-- it is very powerful, but I haven't had the easiest time learning it. Here is a simple example that runs a couple functions periodically (at different rates) using LoopingCall.

For more information, here are the Twisted docs for LoopingCall.


 

  1. from datetime import datetime
  2. from twisted.internet.task import LoopingCall
  3. from twisted.internet import reactor

  4. def hyper_task():
  5.     print "I like to run fast", datetime.now()

  6. def tired_task():
  7.     print "I want to run slowly", datetime.now()

  8. lc = LoopingCall(hyper_task)
  9. lc.start(0.1)

  10. lc2 = LoopingCall(tired_task)
  11. lc2.start(0.5)

  12. reactor.run()


 

Results:


 

  1. I like to run fast 2008-10-14 15:51:02.449537
  2. I want to run slowly 2008-10-14 15:51:02.449915
  3. I like to run fast 2008-10-14 15:51:02.551972
  4. I like to run fast 2008-10-14 15:51:02.652013
  5. I like to run fast 2008-10-14 15:51:02.752006
  6. I like to run fast 2008-10-14 15:51:02.852008
  7. I like to run fast 2008-10-14 15:51:02.952487
  8. I want to run slowly 2008-10-14 15:51:02.952681
  9. I like to run fast 2008-10-14 15:51:03.052012
  10. I like to run fast 2008-10-14 15:51:03.152012
  11. I like to run fast 2008-10-14 15:51:03.252010
  12. I like to run fast 2008-10-14 15:51:03.352009
  13. I like to run fast 2008-10-14 15:51:03.452008
  14. I want to run slowly 2008-10-14 15:51:03.452206
  15. I like to run fast 2008-10-14 15:51:03.552009
  16. I like to run fast 2008-10-14 15:51:03.652013


 

Using TimerService with twistd

To create a daemon with twistd that achieves the same effect, use TimerService. TimerService runs LoopingCall under the hood. It is meant to be used with the Twisted Application infrastructure. See also the documentation on TimerService.

timerservice_ex.py


 

  1. from datetime import datetime
  2. from twisted.application import service
  3. from twisted.application.internet import TimerService

  4. def tired_task():
  5.     print "I want to run slowly", datetime.now()

  6. application = service.Application("myapp")
  7. ts = TimerService(0.5, tired_task)
  8. ts.setServiceParent(application)

Run it:

  1. $ twistd -y timerservice_ex.py



 

  1. 2010-09-20 18:53:50-0700 [-] Log opened.
  2. 2010-09-20 18:53:50-0700 [-] using set_wakeup_fd
  3. 2010-09-20 18:53:50-0700 [-] twistd 10.1.0 (/home/saltycrane/.virtualenvs/default/bin/python 2.6.5) starting up.
  4. 2010-09-20 18:53:50-0700 [-] reactor class: twisted.internet.selectreactor.SelectReactor.
  5. 2010-09-20 18:53:50-0700 [-] I want to run slowly 2010-09-20 18:53:50.896477
  6. 2010-09-20 18:53:51-0700 [-] I want to run slowly 2010-09-20 18:53:51.397043
  7. 2010-09-20 18:53:51-0700 [-] I want to run slowly 2010-09-20 18:53:51.897087
  8. 2010-09-20 18:53:52-0700 [-] I want to run slowly 2010-09-20 18:53:52.397047
  9. 2010-09-20 18:53:52-0700 [-] I want to run slowly 2010-09-20 18:53:52.897068
  10. 2010-09-20 18:53:53-0700 [-] I want to run slowly 2010-09-20 18:53:53.397073
  11. 2010-09-20 18:53:53-0700 [-] I want to run slowly 2010-09-20 18:53:53.897032
  12. 2010-09-20 18:53:54-0700 [-] I want to run slowly 2010-09-20 18:53:54.397083

文章来自:http://www.saltycrane.com/blog/2008/10/running-functions-periodically-using-twisteds-loopingcall/

阅读(852) | 评论(0) | 转发(0) |
0

上一篇:twisted 的 LoopingCall

下一篇:Twisted links

给主人留下些什么吧!~~