Search code examples
pythontwisted

How to use LoopingCall with threads?


I have a simple example:

from twisted.internet import utils, reactor
from twisted.internet import defer
from twisted.internet import threads
from twisted.internet.task import LoopingCall,deferLater
import time

def test1():
    print 'test'

def test2(res):
       l = []
       for i in xrange(3):
           l.append(threads.deferToThread(test4))
       return defer.DeferredList(l)

def test3(res):
    pass

def test4():
    print 'thread start'
    time.sleep(10)
    print 'thread stop'


def loop():
    d = defer.maybeDeferred(test1)
    d = d.addCallback(test2)
    d.addCallback(test3)

LoopingCall(loop).start(2)

reactor.run()

it's script not correct work. I want to:

1) print 'test'
2) start 3 threads, waiting while all threads stops
3) sleep 2 seconds 
4) repeat

Solution

  • LoopingCall will run the callable you pass to it every N seconds, where N is the number you pass to start. It doesn't wait N seconds after the previous call finishes, it waits N seconds after the previous call started. In other words, it tries to stay on the interval defined by N and the starting time, running a call at N seconds, N * 2 seconds, N * 3 seconds, etc.

    If the process is too busy for it to make one of the calls, it will skip that iteration. If the call returns a Deferred and the Deferred has not fired by the next interval, it will skip that iteration.

    So, you can get closer to your desired behavior by returning d at the end of loop, but LoopingCall isn't going to always wait 2 seconds after the Deferred fires. It will wait for the next multiple of N seconds, counting from the start time, and call the function again then.