Differences between revisions 1 and 2
Revision 1 as of 2004-09-11 02:42:25
Size: 1446
Editor: Jerry Marx
Comment: 第一稿
Revision 2 as of 2009-12-25 07:12:52
Size: 1448
Editor: localhost
Comment: converted to 1.6 markup
Deletions are marked like this. Additions are marked like this.
Line 6: Line 6:
-- Jerry Marx 于 [[[DateTime(2004-09-11T02:42:25Z)]]] 最后编辑该页
[[TableOfContents]]
-- Jerry Marx 于 [<<DateTime(2004-09-11T02:42:25Z)>>] 最后编辑该页
<<TableOfContents>>
Line 57: Line 57:
[http://wiki.woodpecker.org.cn/moin.cgi/PyTwisted_2fLowLevelNetworkingEventLoop (目录)Index] [[http://wiki.woodpecker.org.cn/moin.cgi/PyTwisted_2fLowLevelNetworkingEventLoop|(目录)Index]]

为未来安排任务(Scheduling tasks for the future)

-- Jerry Marx 于 [2004-09-11 02:42:25] 最后编辑该页

Contents

Let's say we want to run a task X seconds in the future. The way to do that is defined in the reactor interface twisted.internet.interfaces.IReactorTime:

我们想再x秒后执行一个任务,可以使用twisted.internet.interfaces.IReactorTime:

   1 from twisted.internet import reactor
   2 
   3 def f(s):
   4     print "this will run 3.5 seconds after it was scheduled: %s" % s
   5 
   6 reactor.callLater(3.5, f, "hello, world")

If we want a task to run every X seconds repeatedly, we can use twisted.internet.task.LoopingCall:

如果想每x秒就重复执行一个任务,可以使用twisted.internet.task.LoopingCall:

   1 from twisted.internet import task
   2 
   3 def runEverySecond():
   4     print "a second has passed"
   5 
   6 l = task.LoopingCall(runEverySecond)
   7 l.start(1.0) # call every second
   8 
   9 # l.stop() will stop the looping calls

If we want to cancel a task that we've scheduled:

如果想要取消一个已经安排的任务:

   1 from twisted.internet import reactor
   2 
   3 def f():
   4     print "I'll never run."
   5 
   6 callID = reactor.callLater(5, f)
   7 callID.cancel()
  • 翻译 -- Jerry Marx.

(目录)Index

Version: 1.3.0

PyTwisted/LowLevelNetworkingEventLoop/LLNEL8 (last edited 2009-12-25 07:12:52 by localhost)