##language:zh ''' 为未来安排任务(Scheduling tasks for the future) ''' -- Jerry Marx 于 [<>] 最后编辑该页 <> 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: {{{ #!python from twisted.internet import reactor def f(s): print "this will run 3.5 seconds after it was scheduled: %s" % s 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: {{{ #!python from twisted.internet import task def runEverySecond(): print "a second has passed" l = task.LoopingCall(runEverySecond) l.start(1.0) # call every second # l.stop() will stop the looping calls }}} If we want to cancel a task that we've scheduled: 如果想要取消一个已经安排的任务: {{{ #!python from twisted.internet import reactor def f(): print "I'll never run." callID = reactor.callLater(5, f) callID.cancel() }}} 翻译 -- Jerry Marx. [[http://wiki.woodpecker.org.cn/moin.cgi/PyTwisted_2fLowLevelNetworkingEventLoop|(目录)Index]] Version: 1.3.0