[Twisted-Python] LoopingCall question

Mike Preshman mpresh at gmail.com
Wed Jun 25 23:29:00 EDT 2008


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. Right now I am throwing an exception
which is being caught by an errback. I wait for all three deferred chains to
via a DeferredList with (defer.gatherResults) and then I add a callback to
that in order to stop the reactor.

class PollingException(twisted.python.failure.Failure):

    def __init__(self, var):
        self.var = var

def poll(*args):
    print "poll", args
    raise PollingException(args[1])

def errorHandle(failure):
    print "poll exited"
    return True

def stopReactor(result):
    print "about to stop reactor"

t1 = task.LoopingCall(poll, "A")
d1 = t1.start(1, 3)

t2 = task.LoopingCall(poll, "B")
d2 = t2.start(2, 3)

t3 = task.LoopingCall(poll, "C")
d3 = t3.start(3, 3)

dL = defer.gatherResults([d1, d2, d3])
dL.addCallback(lambda _: reactor.stop())


However, when I run the code and I try to stop the reactor, I get the
following exception which I find puzzling.

poll ('A',)
poll exited
poll ('B',)
poll exited
poll ('C',)
poll exited
Unhandled error in Deferred:
Traceback (most recent call last):
  File "twisted_poll.py", line 36, in <module>
    dL.addCallback(lambda _: reactor.stop())
  File "c:\python25\lib\site-packages\twisted\internet\defer.py", line 191,
in addCallback
  File "c:\python25\lib\site-packages\twisted\internet\defer.py", line 182,
in addCallbacks
--- <exception caught here> ---
  File "c:\python25\lib\site-packages\twisted\internet\defer.py", line 317,
in _runCallbacks
    self.result = callback(self.result, *args, **kw)
  File "twisted_poll.py", line 36, in <lambda>
    dL.addCallback(lambda _: reactor.stop())
  File "c:\python25\lib\site-packages\twisted\internet\base.py", line 342,
in stop
    raise RuntimeError, "can't stop reactor that isn't running"
exceptions.RuntimeError: can't stop reactor that isn't running

Thanks very much,
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-python/attachments/20080625/c137d0b6/attachment.htm 

More information about the Twisted-Python mailing list