Scheduling tasks for the future

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:

from twisted.internet import reactor

def f(s):
    print "this will run in 3.5 seconds: %s" % s

reactor.callLater(3.5, f, "hello, world")

If we want a task to run every X seconds repeatedly, we can just re-add it every time it's run:

from twisted.internet import reactor

def runEverySecond():
    print "a second has passed"
    reactor.callLater(1, runEverySecond)

reactor.callLater(1, runEverySecond)

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

from twisted.internet import reactor

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

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