Hi folks,
short question, I implement a very custom statefull protocol which need to have some timers in it. Can someone point me to examples how to do that?
A.
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.
David
Andrzej Leszczynski wrote:
Hi folks,
short question, I implement a very custom statefull protocol which need to have some timers in it. Can someone point me to examples how to do that?
A.
Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
--- David Guaraglia dguaraglia@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.
A.
Andrzej,
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: x.sendKeepAlive()) 5) create a LoopingCall in your factory __init__.
Maybe it's not the best solution, but it worked for me.
David
Andrzej Leszczynski wrote:
--- David Guaraglia dguaraglia@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.
A.
On Thu, 21 Jul 2005 12:49:21 -0300, David Guaraglia dguaraglia@gmx.de wrote:
Andrzej,
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:
- add an empty list (connectionsList or whatever) to your factory
- 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: x.sendKeepAlive()) 5) create a LoopingCall in your factory __init__.
Maybe it's not the best solution, but it worked for me.
Where's the thread? (Hint: not in the LoopingCall)
Jp
LOL! (lols allowed here? or just not allowed as in #twisted?). Don't know why I used the word "thread" in there... I was just going to describe something I had made, then I remembered I had corrected it using a LoopingCall.
David
Jp Calderone wrote:
On Thu, 21 Jul 2005 12:49:21 -0300, David Guaraglia dguaraglia@gmx.de wrote:
Andrzej,
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:
- add an empty list (connectionsList or whatever) to your factory
- 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: x.sendKeepAlive()) 5) create a LoopingCall in your factory __init__.
Maybe it's not the best solution, but it worked for me.
Where's the thread? (Hint: not in the LoopingCall)
Jp
Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
2005/7/21, Andrzej Leszczynski leszczynscy@yahoo.com:
Hi folks,
short question, I implement a very custom statefull protocol which need to have some timers in it. Can someone point me to examples how to do that?
I've written a ResourceManager which check frequently and release resources if necessary, may the following code snippet helpful to you :)
[code] from twisted.internet import task class ResourceManager: def __init__(self): interval = 5 # check every 5 seconds releaser = task.LoopingCall(self._releaseResources) releaser.start(interval)
def _releaseResource(self): [snip] ... [/code]
--- Eric Hsu nkeric@gmail.com wrote:
I've written a ResourceManager which check frequently and release resources if necessary, may the following code snippet helpful to you :)
[...]
releaser.start(interval)
[...]
I will try it out. Seems to be close to what I need. One question thought?
How it is implemented under the hood, (I hope it does not spawn any new threads).
A.