1
# -*- test-case-name: twisted.conch.test.test_mixin -*-
2
# Copyright (c) 2001-2004 Twisted Matrix Laboratories.
3
# See LICENSE for details.
5
"""Experimental optimization
7
This module provides a single mixin class which allows protocols to
8
collapse numerous small writes into a single larger one.
10
API Stability: Unstable
12
@author: U{Jp Calderone<mailto:exarkun@twistedmatrix.com>}
15
from twisted.internet import reactor
18
"""Mixin which adds write buffering.
20
_delayedWriteCall = None
26
return reactor.callLater(self.DELAY, self.flush)
28
def reschedule(self, token):
29
token.reset(self.DELAY)
31
def write(self, bytes):
32
"""Buffer some bytes to be written soon.
34
Every call to this function delays the real write by C{self.DELAY}
35
seconds. When the delay expires, all collected bytes are written
36
to the underlying transport using L{ITransport.writeSequence}.
38
if self._delayedWriteCall is None:
40
self._delayedWriteCall = self.schedule()
42
self.reschedule(self._delayedWriteCall)
43
self.bytes.append(bytes)
46
"""Flush the buffer immediately.
48
self._delayedWriteCall = None
49
self.transport.writeSequence(self.bytes)