Search code examples
pythontwisted

Python: How do I change this code so that I can doWork() once in every 60 seconds?


How do I change this code so that I can call doWork() once in every 60 seconds. Currently, it is looping in definitely and stops after 60 seconds. Want to do the exact reverse.

from twisted.internet import task
from twisted.internet import reactor
import twapi
timeout = 200.0 # Sixty seconds

def doWork():
    #do work here
    twapi.main('1')
    pass

l = task.LoopingCall(doWork)
l.start(timeout) # call every sixty seconds

reactor.run()

Solution

  • From oficial Twisted documentation

    from twisted.internet import task
    from twisted.internet import reactor
    
    def runEverySecond():
        print "a second has passed"
    
    l = task.LoopingCall(runEverySecond)
    l.start(1.0) # call every second
    
    reactor.run()