Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mixin.py 1.3KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. # -*- test-case-name: twisted.conch.test.test_mixin -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. Experimental optimization
  6. This module provides a single mixin class which allows protocols to
  7. collapse numerous small writes into a single larger one.
  8. @author: Jp Calderone
  9. """
  10. from twisted.internet import reactor
  11. class BufferingMixin:
  12. """
  13. Mixin which adds write buffering.
  14. """
  15. _delayedWriteCall = None
  16. data = None
  17. DELAY = 0.0
  18. def schedule(self):
  19. return reactor.callLater(self.DELAY, self.flush)
  20. def reschedule(self, token):
  21. token.reset(self.DELAY)
  22. def write(self, data):
  23. """
  24. Buffer some bytes to be written soon.
  25. Every call to this function delays the real write by C{self.DELAY}
  26. seconds. When the delay expires, all collected bytes are written
  27. to the underlying transport using L{ITransport.writeSequence}.
  28. """
  29. if self._delayedWriteCall is None:
  30. self.data = []
  31. self._delayedWriteCall = self.schedule()
  32. else:
  33. self.reschedule(self._delayedWriteCall)
  34. self.data.append(data)
  35. def flush(self):
  36. """
  37. Flush the buffer immediately.
  38. """
  39. self._delayedWriteCall = None
  40. self.transport.writeSequence(self.data)
  41. self.data = None