Running a function periodically in twisted protocol
You would probably want to do this in the Factory for the connections. The Factory is not automatically notified of every time a connection is made and lost, so you can notify it from the Protocol.
Here is a complete example of how to use twisted.internet.task.LoopingCall in conjunction with a customised basic Factory and Protocol to announce that '10 seconds has passed' to every connection every 10 seconds.
from twisted.internet import reactor, protocol, taskclass MyProtocol(protocol.Protocol): def connectionMade(self): self.factory.clientConnectionMade(self) def connectionLost(self, reason): self.factory.clientConnectionLost(self)class MyFactory(protocol.Factory): protocol = MyProtocol def __init__(self): self.clients = [] self.lc = task.LoopingCall(self.announce) self.lc.start(10) def announce(self): for client in self.clients: client.transport.write("10 seconds has passed\n") def clientConnectionMade(self, client): self.clients.append(client) def clientConnectionLost(self, client): self.clients.remove(client)myfactory = MyFactory()reactor.listenTCP(9000, myfactory)reactor.run()
I'd imagine the easiest way to do that is to manage a list of clients in the protocol with connectionMade and connectionLost in the client and then use a LoopingCall to ask each client to send data.
That feels a little invasive, but I don't think you'd want to do it without the protocol having some control over the transmission/reception. Of course, I'd have to see your code to really know how it'd fit in well. Got a github link? :)