High performance IO on non-blocking sockets

Jp Calderone exarkun at intarweb.us
Sat Mar 15 01:32:34 CET 2003

On Fri, Mar 14, 2003 at 03:22:01PM +0100, Troels Walsted Hansen wrote:
> "Richie Hindle" <richie at entrian.com> wrote in message
> news:lgm37vsi6631fimchqdivsg5vh713cifaj at 4ax.com...
> > From http://www.python.org/doc/current/lib/socket-objects.html :
> > 7.2.1 Socket Objects
> > sendall( string[, flags])
> I should have mentioned that in my original post... sendall() doesn't work
> for non-blocking sockets. :(
> The good news is that Kjetil Jacobsen came up with the perfect solution for
> send().
>   send_buffer = buffer(self.data, self.offset)
>   sent = self.socket.send(send_buffer)
>   self.buffer_offset += sent
> No copying, and all writes are as big as the socket buffer can hold. Cost of
> allocating a buffer object should be very minor in the grand scheme of
> things.

  Have you timed this, vs the original, naive code?  Slicing a string
shouldn't copy any bytes, only create a new string object with a modified
starting pointer and length.  This seems as if it would be about as
expensive as creating a new buffer object, but has the advantage of running
more of the work in C, rather than Python (no name lookup for buffer, for

  I ask because I can't seem to squeeze any speedup out of Twisted by making
this change (in fact, I find a significant slowdown, 12309.8 KB/s using the
original "buf = buf[sent:]" code to 9408.2 KB/s using the buffer()

  I'm hoping I've just screwed something up, of course, and would love to
hear that the buffer() approach is, in fact, much faster :)


Somewhere, something incredible is waiting to be known.
                -- Carl Sagan
 up 11 days, 15:59, 6 users, load average: 0.13, 0.50, 0.42

More information about the Python-list mailing list