[Twisted-Python] First try at thread-safe FileDescriptor

Itamar Shtull-Trauring itamar at zoteca.com
Fri Aug 10 12:46:28 MDT 2001


Moshe Zadka wrote:


 > Please, please, use either diff -u or diff -c. Plain diff is simply
 > unreadable

D'oh. I must've never set it up on my laptop.


Index: abstract.py
===================================================================
RCS file: /cvs/Twisted/twisted/internet/abstract.py,v
retrieving revision 1.2
diff -c -r1.2 abstract.py
*** abstract.py	2001/07/09 19:59:34	1.2
--- abstract.py	2001/08/10 18:47:46
***************
*** 1,6 ****
-
   # System Imports
! import types

   # Twisted Imports
   from twisted.python import threadable, log
--- 1,5 ----
   # System Imports
! import types, operator, string

   # Twisted Imports
   from twisted.python import threadable, log
***************
*** 19,25 ****
       producer = None
       disconnected = 0
       __reap = 0
!
       def connectionLost(self):
           """The connection was lost.

--- 18,27 ----
       producer = None
       disconnected = 0
       __reap = 0
!
!     def __init__(self):
!         self.unsentChunks = []
!
       def connectionLost(self):
           """The connection was lost.

***************
*** 51,56 ****
--- 53,66 ----
       def doWrite(self):
           """Called when data is available for writing.
           """
+         numChunks = len(self.unsentChunks)
+         if numChunks:
+             chunks = [self.unsent,]
+             for i in range(numChunks):
+                 chunks.append(self.unsentChunks.pop(0))
+
+             self.unsent = string.join(chunks, '')
+
           # Send as much data as you can.
           l = self.writeSomeData(self.unsent)
           if l < 0:
***************
*** 78,91 ****
           ready for writing.  If necessary, it will wake up the I/O
thread to add
           this FileDescriptor for writing, that the write will happen as
soon as
           possible.
           """
           assert type(data) == types.StringType, "Data must be a string."
           if not self.connected:
               return
           if data:
!             self.unsent = self.unsent + data
               if self.producer is not None:
!                 if len(self.unsent) > self.bufferSize:
                       self.producerPaused = 1
                       self.producer.pauseProducing()
               self.startWriting()
--- 88,103 ----
           ready for writing.  If necessary, it will wake up the I/O
thread to add
           this FileDescriptor for writing, that the write will happen as
soon as
           possible.
+
+         This method is thread-safe.
           """
           assert type(data) == types.StringType, "Data must be a string."
           if not self.connected:
               return
           if data:
!             self.unsentChunks.append(data)
               if self.producer is not None:
!                 if len(self.unsent)  + reduce(operator.add, map(len,
self.unsentChunks)) > self.bufferSize:
                       self.producerPaused = 1
                       self.producer.pauseProducing()
               self.startWriting()

Index: tcp.py
===================================================================
RCS file: /cvs/Twisted/twisted/internet/tcp.py,v
retrieving revision 1.10
diff -c -r1.10 tcp.py
*** tcp.py	2001/08/10 06:18:11	1.10
--- tcp.py	2001/08/10 18:47:46
***************
*** 39,44 ****
--- 39,45 ----
       selectable (those that use a TCP/IP or similiar socket).
       """
       def __init__(self, skt, protocol):
+         abstract.FileDescriptor.__init__(self)
           self.socket = skt
           self.socket.setblocking(0)
           self.fileno = skt.fileno
***************
*** 225,232 ****
           """Initialize with a numeric port to listen on.
           """
           self.port = port
! 	self.factory = factory
! 	self.backlog = backlog

       def __repr__(self):
           return "<%s on %s>" % (self.factory.__class__, self.port)
--- 226,233 ----
           """Initialize with a numeric port to listen on.
           """
           self.port = port
!         self.factory = factory
!         self.backlog = backlog

       def __repr__(self):
           return "<%s on %s>" % (self.factory.__class__, self.port)






More information about the Twisted-Python mailing list