[Twisted-Python] Polling from Twisted

Juanjo Conti jjconti at gmail.com
Tue Apr 28 07:11:41 EDT 2009


Thanks both for th soon replies.


My code looks a lot like Lucas suggested (I was messing the stop stuff).

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?

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

2009/4/28 Lucas Taylor <ltaylor.volks at gmail.com>

> On 4/27/09 5:06 PM, Juanjo Conti wrote:
> > Hi, this is my first mail to the list.
> >
> > I am writing a server using Twisted, extending LineOnlyReceiver. In it I
> > maintain a list of clients connected.
> >
> > I'd like to poll every client for certain status information every 30
> > seconds. Which is the correct approach to implement this?
> >
>
> A simple option is to setup a task.LoopingCall in your factory to
> iterate over the list of clients every 30 seconds and issue your status
> request. I don't know if it is correct for your application, but this
> should illustrate the basic idea:
>
>
> from twisted.internet import reactor, task
>
> class StatusPollingReceiver(LineOnlyReceiver):
>    def connectionMade(self):
>        self.factory.clients.append(self)
>
>    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 = []
>
>        # send a status request to each client every 30 seconds
>        self.statusloop = task.LoopingCall(self.requestStatus)
>        self.statusloop.start(30.0)
>
>    def requestStatus(self):
>        for client in self.clients:
>            client.transport.write('stat\r\n')
>
>    def stopFactory(self):
>        self.statusloop.stop()
>
>    def buildProtocol(self, addr):
>        p = StatusPollingReceiver()
>        p.factory = self
>        return p
>
>
>
>
> see:
>
> http://twistedmatrix.com/documents/current/api/twisted.internet.task.LoopingCall.html
>
>
>
> _______________________________________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
>



-- 
Juanjo Conti
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-python/attachments/20090428/b1ab2e88/attachment.htm 


More information about the Twisted-Python mailing list