[Twisted-Python] Proposed change to buffering

Itamar Shtull-Trauring itamar at itamarst.org
Mon Oct 4 09:36:42 MDT 2004


Now that we don't do write-at-once, lists are easier to use. 

Benefits: Allows protocols with many small writes to not have to do
their own buffering (e.g. http, imap4 as currently coded, PB, IRC,
telnet, etc. etc.)

Problems: Bit more overhead for large writes.

There's also a bit of code change in tcp.py for the SSL code.

Index: abstract.py
===================================================================
--- abstract.py	(revision 11908)
+++ abstract.py	(working copy)
@@ -43,7 +43,9 @@
         if not reactor:
             from twisted.internet import reactor
         self.reactor = reactor
-
+        self._tempDataBuffer = [] # will be added to dataBuffer in doWrite
+        self._tempDataLen = 0
+    
     def connectionLost(self, reason):
         """The connection was lost.
 
@@ -81,6 +83,9 @@
         there; a result of 0 implies no write was done, and a result of None
         indicates that a write was done.
         """
+        self.dataBuffer += "".join(self._tempDataBuffer)
+        self._tempDataBuffer = []
+        self._tempDataLen = 0
         # Send as much data as you can.
         if self.offset:
             l = self.writeSomeData(buffer(self.dataBuffer, self.offset))
@@ -131,15 +136,16 @@
         if not self.connected:
             return
         if data:
-            self.dataBuffer = self.dataBuffer + data
+            self._tempDataBuffer.append(data)
+            self._tempDataLen += len(data)
             if self.producer is not None:
-                if len(self.dataBuffer) > self.bufferSize:
+                if len(self.dataBuffer) + self._tempDataLen > self.bufferSize:
                     self.producerPaused = 1
                     self.producer.pauseProducing()
             self.startWriting()
 
     def writeSequence(self, iovec):
-        self.write("".join(iovec))
+        self._tempDataBuffer.extend(iovec)
 
     def loseConnection(self):
         """Close the connection at the next available opportunity.






More information about the Twisted-Python mailing list