[Twisted-Python] Polling from Twisted

Lucas Taylor ltaylor.volks at gmail.com
Tue Apr 28 13:28:14 EDT 2009


On 4/28/09 4:11 AM, Juanjo Conti wrote:
> 
> Another question. Thinking twisted (and thinking about performance),
> it's ok to loop every 30 seconds over the clients asking their status or
> should be better to schudle a new polling rutine starting 30 seconds
> from now every time a client is connected? In the second approach the
> polls will be more spread? Is a good Idea?


If this basic approach works for your application, then either way seems
simple enough to swap out and test. Whether it is a good idea or not
depends on your application, the # of clients, etc.

I don't think you're going to find it makes much of a difference for
some low number of connected clients, so keeping a single timer in the
factory seems easier to manage.

But if you need more control and want to make sure your clients aren't
being polled before it is necessary (i.e. < 30s from connect), then your
second approach might be better.

Lucas

> It will be something like:
> 
> from twisted.internet import reactor, task
> 
> class StatusPollingReceiver(LineOnlyReceiver):
>    def requestStatus(self):
>        self.transport.write('stat\r\n')
>           
>    def connectionMade(self):
>        self.factory.clients.append(self)
>        statusloop = task.LoopingCall(self.requestStatus)
>        statusloop.start(30.0)
> 
>    def lineReceived(self, line):
>        print('Rx: %s' % line)
> 
>    def connectionLost(self, reason):
>        self.factory.clients.remove(self)
> 
> 
> class StatusPollingFactory(Factory):
> 
>    def __init__(self):
>        self.clients = []
> 
>    def stopFactory(self):
>        self.statusloop.stop()
> 
>    def buildProtocol(self, addr):
>        p = StatusPollingReceiver()
>        p.factory = self
>        return p




More information about the Twisted-Python mailing list