# 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 3.5 seconds after it was scheduled: %s" % s

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

    # f() will only be called if the event loop was started:
    reactor.run()

If we want a task to run every X seconds repeatedly, we can use `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
    reactor.run()

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()
    reactor.run()

As with all reactor-based code, in order for scheduling to work the reactor must be started using `reactor.run()`.

Index

Version: 2.1.0