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

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

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")


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

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


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

from twisted.internet import reactor

def f():
    print "I'll never run."

callID = reactor.callLater(5, f)
callID.cancel()

posted on 2012-05-16 16:18  很多不懂呀。。  阅读(751)  评论(0编辑  收藏  举报

导航