Thanks both for th soon replies.<br><br><br>My code looks a lot like Lucas suggested (I was messing the stop stuff).<br><br>Another question. Thinking twisted (and thinking about performance), it&#39;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?<br>
<br>It will be something like:<br><br>from twisted.internet import reactor, task<br><br>class StatusPollingReceiver(LineOnlyReceiver):<br>   def requestStatus(self):<br>       self.transport.write(&#39;stat\r\n&#39;)<br>           <br>
   def connectionMade(self):<br>       self.factory.clients.append(self)<br>       statusloop = task.LoopingCall(self.requestStatus)<br>       statusloop.start(30.0)<br><br>   def lineReceived(self, line):<br>       print(&#39;Rx: %s&#39; % line)<br>
<br>   def connectionLost(self, reason):<br>       self.factory.clients.remove(self)<br><br><br>class StatusPollingFactory(Factory):<br><br>   def __init__(self):<br>       self.clients = []<br><br>   def stopFactory(self):<br>
       self.statusloop.stop()<br><br>   def buildProtocol(self, addr):<br>       p = StatusPollingReceiver()<br>       p.factory = self<br>       return p<br><br><div class="gmail_quote">2009/4/28 Lucas Taylor <span dir="ltr">&lt;<a href="mailto:ltaylor.volks@gmail.com">ltaylor.volks@gmail.com</a>&gt;</span><br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div class="im">On 4/27/09 5:06 PM, Juanjo Conti wrote:<br>
&gt; Hi, this is my first mail to the list.<br>
&gt;<br>
&gt; I am writing a server using Twisted, extending LineOnlyReceiver. In it I<br>
&gt; maintain a list of clients connected.<br>
&gt;<br>
&gt; I&#39;d like to poll every client for certain status information every 30<br>
&gt; seconds. Which is the correct approach to implement this?<br>
&gt;<br>
<br>
</div>A simple option is to setup a task.LoopingCall in your factory to<br>
iterate over the list of clients every 30 seconds and issue your status<br>
request. I don&#39;t know if it is correct for your application, but this<br>
should illustrate the basic idea:<br>
<br>
<br>
from twisted.internet import reactor, task<br>
<br>
class StatusPollingReceiver(LineOnlyReceiver):<br>
    def connectionMade(self):<br>
        self.factory.clients.append(self)<br>
<br>
    def lineReceived(self, line):<br>
        print(&#39;Rx: %s&#39; % line)<br>
<br>
    def connectionLost(self, reason):<br>
        self.factory.clients.remove(self)<br>
<br>
<br>
class StatusPollingFactory(Factory):<br>
<br>
    def __init__(self):<br>
        self.clients = []<br>
<br>
        # send a status request to each client every 30 seconds<br>
        self.statusloop = task.LoopingCall(self.requestStatus)<br>
        self.statusloop.start(30.0)<br>
<br>
    def requestStatus(self):<br>
        for client in self.clients:<br>
            client.transport.write(&#39;stat\r\n&#39;)<br>
<br>
    def stopFactory(self):<br>
        self.statusloop.stop()<br>
<br>
    def buildProtocol(self, addr):<br>
        p = StatusPollingReceiver()<br>
        p.factory = self<br>
        return p<br>
<br>
<br>
<br>
<br>
see:<br>
<a href="http://twistedmatrix.com/documents/current/api/twisted.internet.task.LoopingCall.html" target="_blank">http://twistedmatrix.com/documents/current/api/twisted.internet.task.LoopingCall.html</a><br>
<br>
<br>
<br>
_______________________________________________<br>
Twisted-Python mailing list<br>
<a href="mailto:Twisted-Python@twistedmatrix.com">Twisted-Python@twistedmatrix.com</a><br>
<a href="http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python" target="_blank">http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python</a><br>
</blockquote></div><br><br clear="all"><br>-- <br>Juanjo Conti<br>