code_junkie

Is it possible to set a timeout on a socket in Twisted?

Nov 11th, 2011
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.64 KB | None | 0 0
  1. from twisted.internet import task
  2. from twisted.internet import reactor
  3.  
  4. datagramRecieved = False
  5. timeout = 1.0 # One second
  6.  
  7. # UDP code here
  8.  
  9. def testTimeout():
  10. global datagramRecieved
  11. if not datagramRecieved:
  12. reactor.stop()
  13. datagramRecieved = False
  14.  
  15.  
  16. l = task.LoopingCall(testTimeout)
  17. l.start(timeout) # call every second
  18.  
  19. # l.stop() will stop the looping calls
  20. reactor.run()
  21.  
  22. class Protocol(DatagramProtocol):
  23. def __init__(self, timeout):
  24. self.timeout = timeout
  25.  
  26. def datagramReceived(self, datagram):
  27. self.timeout.cancel()
  28. # ...
  29.  
  30. def datagramReceived(self, datagram):
  31. self.timeout.cancel()
  32. # ...
Add Comment
Please, Sign In to add comment