[Twisted-Python] LoopingCall question
Mike Preshman
mpresh at gmail.com
Wed Jun 25 21:29:00 MDT 2008
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. Right now I am throwing an exception
which is being caught by an errback. I wait for all three deferred chains to
complete
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"
reactor.stop()
t1 = task.LoopingCall(poll, "A")
d1 = t1.start(1, 3)
d1.addErrback(errorHandle)
t2 = task.LoopingCall(poll, "B")
d2 = t2.start(2, 3)
d2.addErrback(errorHandle)
t3 = task.LoopingCall(poll, "C")
d3 = t3.start(3, 3)
d3.addErrback(errorHandle)
dL = defer.gatherResults([d1, d2, d3])
dL.addCallback(lambda _: reactor.stop())
reactor.run()
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
callbackKeywords=kw)
File "c:\python25\lib\site-packages\twisted\internet\defer.py", line 182,
in addCallbacks
self._runCallbacks()
--- <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,
-Mike
-------------- next part --------------
An HTML attachment was scrubbed...
URL: </pipermail/twisted-python/attachments/20080625/c137d0b6/attachment.html>
More information about the Twisted-Python
mailing list