[Twisted-Python] LoopingCall question
Mike Preshman
mpresh at gmail.com
Fri Jun 27 08:06:07 MDT 2008
I got it to work with classes.
from twisted.internet import utils, reactor, defer, task
import twisted
import time, sys
class Looping:
def __init__(self, c, t):
self.c = c
self.t = t
self.count = 0
def myPoll(self, *args):
print "poll", args
self.count = self.count + 1
if self.count > 3:
self.task.stop()
def start(self):
self.task = task.LoopingCall(self.myPoll, self.c)
return self.task.start(self.t)
c1 = Looping("A", 1)
d1 = c1.start()
c2 = Looping("B", 2)
d2 = c2.start()
c3 = Looping("C", 3)
d3 = c3.start()
dL = defer.gatherResults([d1, d2, d3])
dL.addCallback(lambda _: reactor.stop())
reactor.run()
On Fri, Jun 27, 2008 at 9:44 AM, Itamar Shtull-Trauring <itamar at itamarst.org>
wrote:
> 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
>
>
>
> _______________________________________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: </pipermail/twisted-python/attachments/20080627/d817e7f6/attachment.html>
More information about the Twisted-Python
mailing list