[Twisted-Python] How to run function from protocol class

I got the code: from twisted.internet.protocol import Protocol class MyClientProtocol(Protocol): buffer = '' def connectionMade(self): self.transport.write('hello world') def myFunction(self): self.transport.write('blah') def dataReceived(self, data): self.buffer += data if self.buffer == 'hello world': self.transport.loseConnection() from twisted.internet.protocol import ClientFactory class MyFactory(ClientFactory): protocol = MyClientProtocol def startedConnecting(self, connector): pass # we could connector.stopConnecting() def clientConnectionLost(self, connector, reason): connector.connect() # reconnect def clientConnectionFailed(self, connector, reason): print "connection failed" reactor.stop() from twisted.internet import reactor, task #t=MyClientProtocol() #l = task.LoopingCall(t.myFunction) #l.start(5.0) reactor.connectTCP('localhost', 7771, MyFactory(), timeout=30) reactor.run() I want to run my function myFunction every 5 sec (commented code). I want to use a task.LoopingCall loop. I am run out of new ideas. I don't know how to do this. Help please.

AFAIK, the Factory class creates a Protocol instance for each connection, then if you would like to achieve what you want, you'll have to do the looping call _inside_ the Protocol class. So I guess you could try this to see if it works: class MyClientProtocol(Protocol): ... def connectionMade(self): ... l = task.LoopingCall(t.myFunction) l.start(5.0) [snip] I'm not sure whether we can do this in the __init__(self) method, I guess that would failed since the protocol's transport instance haven't even been created during __init__... so it would be much better to do that inside the connectionMade(self) method. Hope that helps : ) (sorry for my poor english ;p ) - Eric 2005/8/12, Michał Tyde <ajchos@wp.pl>:

AFAIK, the Factory class creates a Protocol instance for each connection, then if you would like to achieve what you want, you'll have to do the looping call _inside_ the Protocol class. So I guess you could try this to see if it works: class MyClientProtocol(Protocol): ... def connectionMade(self): ... l = task.LoopingCall(t.myFunction) l.start(5.0) [snip] I'm not sure whether we can do this in the __init__(self) method, I guess that would failed since the protocol's transport instance haven't even been created during __init__... so it would be much better to do that inside the connectionMade(self) method. Hope that helps : ) (sorry for my poor english ;p ) - Eric 2005/8/12, Michał Tyde <ajchos@wp.pl>:
participants (3)
-
Eric Hsu
-
Jp Calderone
-
Michał Tyde