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.
- from datetime import datetime
- from twisted.internet.task import LoopingCall
- from twisted.internet import reactor
- def hyper_task():
- print "I like to run fast", datetime.now()
- def tired_task():
- print "I want to run slowly", datetime.now()
- lc = LoopingCall(hyper_task)
- lc.start(0.1)
- lc2 = LoopingCall(tired_task)
- lc2.start(0.5)
- reactor.run()
Results:
- I like to run fast 2008-10-14 15:51:02.449537
- I want to run slowly 2008-10-14 15:51:02.449915
- I like to run fast 2008-10-14 15:51:02.551972
- I like to run fast 2008-10-14 15:51:02.652013
- I like to run fast 2008-10-14 15:51:02.752006
- I like to run fast 2008-10-14 15:51:02.852008
- I like to run fast 2008-10-14 15:51:02.952487
- I want to run slowly 2008-10-14 15:51:02.952681
- I like to run fast 2008-10-14 15:51:03.052012
- I like to run fast 2008-10-14 15:51:03.152012
- I like to run fast 2008-10-14 15:51:03.252010
- I like to run fast 2008-10-14 15:51:03.352009
- I like to run fast 2008-10-14 15:51:03.452008
- I want to run slowly 2008-10-14 15:51:03.452206
- I like to run fast 2008-10-14 15:51:03.552009
- 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
- from datetime import datetime
- from twisted.application import service
- from twisted.application.internet import TimerService
- def tired_task():
- print "I want to run slowly", datetime.now()
- application = service.Application("myapp")
- ts = TimerService(0.5, tired_task)
- ts.setServiceParent(application)
Run it:
- $ twistd -y timerservice_ex.py
- 2010-09-20 18:53:50-0700 [-] Log opened.
- 2010-09-20 18:53:50-0700 [-] using set_wakeup_fd
- 2010-09-20 18:53:50-0700 [-] twistd 10.1.0 (/home/saltycrane/.virtualenvs/default/bin/python 2.6.5) starting up.
- 2010-09-20 18:53:50-0700 [-] reactor class: twisted.internet.selectreactor.SelectReactor.
- 2010-09-20 18:53:50-0700 [-] I want to run slowly 2010-09-20 18:53:50.896477
- 2010-09-20 18:53:51-0700 [-] I want to run slowly 2010-09-20 18:53:51.397043
- 2010-09-20 18:53:51-0700 [-] I want to run slowly 2010-09-20 18:53:51.897087
- 2010-09-20 18:53:52-0700 [-] I want to run slowly 2010-09-20 18:53:52.397047
- 2010-09-20 18:53:52-0700 [-] I want to run slowly 2010-09-20 18:53:52.897068
- 2010-09-20 18:53:53-0700 [-] I want to run slowly 2010-09-20 18:53:53.397073
- 2010-09-20 18:53:53-0700 [-] I want to run slowly 2010-09-20 18:53:53.897032
- 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/
阅读(860) | 评论(0) | 转发(0) |