code_junkie

Running a function periodically in twisted protocol

Nov 12th, 2011
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. from twisted.internet import reactor, protocol, task
  2.  
  3. class MyProtocol(protocol.Protocol):
  4. def connectionMade(self):
  5. self.factory.clientConnectionMade(self)
  6. def connectionLost(self, reason):
  7. self.factory.clientConnectionLost(self)
  8.  
  9. class MyFactory(protocol.Factory):
  10. protocol = MyProtocol
  11. def __init__(self):
  12. self.clients = []
  13. self.lc = task.LoopingCall(self.announce)
  14. self.lc.start(10)
  15.  
  16. def announce(self):
  17. for client in self.clients:
  18. client.transport.write("10 seconds has passedn")
  19.  
  20. def clientConnectionMade(self, client):
  21. self.clients.append(client)
  22.  
  23. def clientConnectionLost(self, client):
  24. self.clients.remove(client)
  25.  
  26. myfactory = MyFactory()
  27. reactor.listenTCP(9000, myfactory)
  28. reactor.run()
Add Comment
Please, Sign In to add comment