[Twisted-Python] timers

David Guaraglia dguaraglia at gmx.de
Thu Jul 21 11:49:21 EDT 2005


I've done something very similar using just one thread for the protocol. 
But I'm not really sure this is what you need. What I did was use a 
LoopingCall every 2 minutes that would call a method in my protocol's 
factory. Everytime I got a new connection (connectionMade event in the 
Protocol) I'd append the protocol to a list in the factory, and then I'd 
remove it when the protocol is closed.

So the sequence is:

1) add an empty list (connectionsList or whatever) to your factory
2) add "self.factory.connectionsList.append(self)" in connectionMade in 
your protocol
3) add "self.factory.connectionsList.remove(self)" in connectionLost in 
your protocol
4) add a method that sends your "keep alive" to every connection in 
connectionsList in your factory. (for x in self.connectionsList: 
5) create a LoopingCall in your factory __init__.

Maybe it's not the best solution, but it worked for me.


Andrzej Leszczynski wrote:

>--- David Guaraglia <dguaraglia at gmx.de> wrote:
>>What will you exactly do with the timers? Depending
>>on the use you could 
>>replace the timers with a thread, or a deferred, or
>>a TimeoutMixin.
>Threads are out of the question. There going to be a
>clas deriving from a Protocol used over TCP/IP. E.g.
>there is long lasting connection with the
>client/server and a need to send kind of heart beat
>messages every given period of time as well as pegging
>some stat info.

More information about the Twisted-Python mailing list