[Twisted-Python] LoopingCall question

Itamar Shtull-Trauring itamar at itamarst.org
Fri Jun 27 07:44:07 MDT 2008


On Wed, 2008-06-25 at 23:29 -0400, Mike Preshman wrote:
> Hello,
> 
> I am trying to run three deferred LoopingCall chains in parallel.
> Here is my code below. I am trying to figure out what is the correct
> way of exiting the polling after I meet some 
> condition that the polling satisfies.

You needn't use LoopingCall, if it's unsuitable - assuming Twisted 8.1:


from twisted.internet import defer, reactor, task

class _LoopUntil(object):

    def __init__(self, delay, check):
        self.delay = delay
        self.check = check
        self.result = defer.Deferred()

    def go(self):
        task.deferLater(reactor, self.delay, check).addCallbacks(
            self._gotResult, self._failed)
    
    def _gotResult(self, r):
         if r:
             d = self.result
             del self.result
             self.result.callback(r)
         else:
             self.go()

    def _failed(self, f):
        self.result.errback(f)
        del self.result


def loopUntil(delay, check):
    l = _LoopUntil(delay, check)
    r = l.result
    l.go()
    return r






More information about the Twisted-Python mailing list