我们想再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()