1 # -*- test-case-name: twisted.conch.test.test_mixin -*-
2 # Copyright (c) Twisted Matrix Laboratories.
3 # See LICENSE for details.
6 Experimental optimization
8 This module provides a single mixin class which allows protocols to
9 collapse numerous small writes into a single larger one.
14 from twisted.internet import reactor
17 """Mixin which adds write buffering.
19 _delayedWriteCall = None
25 return reactor.callLater(self.DELAY, self.flush)
27 def reschedule(self, token):
28 token.reset(self.DELAY)
30 def write(self, bytes):
31 """Buffer some bytes to be written soon.
33 Every call to this function delays the real write by C{self.DELAY}
34 seconds. When the delay expires, all collected bytes are written
35 to the underlying transport using L{ITransport.writeSequence}.
37 if self._delayedWriteCall is None:
39 self._delayedWriteCall = self.schedule()
41 self.reschedule(self._delayedWriteCall)
42 self.bytes.append(bytes)
45 """Flush the buffer immediately.
47 self._delayedWriteCall = None
48 self.transport.writeSequence(self.bytes)