[Twisted-Python] Run a function repeatedly at different intervals

Jean-Paul Calderone exarkun at divmod.com
Wed May 3 11:03:50 EDT 2006


On Wed, 3 May 2006 10:49:19 -0400, Khoa Nguyen <khoa.coffee at gmail.com> wrote:
>Hi,
>
>I am new to Twisted Python. I would like to fire a function several
>times at different intervals. For example, at 1 second fire f(), at 2
>second fire f() again, at 4 second fire f(), and so on...
>
>How do I do that? I looked at the doc and it has
>task.LoopingCall(second) but the time interval is fixed...
>

LoopingCall is implemented in terms of IReactorTime.callLater.  If you want to schedule a function to run in 1, 2, and 4 seconds:

    reactor.callLater(1, f)
    reactor.callLater(2, f)
    reactor.callLater(4, f)

You could also have f re-schedule itself when it runs, or write a helper that schedules it, as LoopingCall is a helper for running a function on a fixed interval.

It may also be possible to extend LoopingCall to allow changing its interval as it runs.  You could also use LoopingCall as it is and stop() and then start() it with a new interval.

Jean-Paul




More information about the Twisted-Python mailing list