Any clues to source of this delay?

Bruce Dodson bruce_dodson at bigfoot.com
Thu Aug 5 22:37:06 EDT 1999


This isn't unique to Python.  It has to do with the way TCP buffers data
within a time window by default.  The data is sent when some threshold size
is surpassed or when the time window expires (and I guess you saw that
sending zero bytes can force it to flush on some platforms).  For many
applications this buffering gives a performance improvement, but in your
case (the kind of application that wants to do lots round-trips per second
with small amounts of data in each request), it kills performance.

To optimize a socket for this kind of conversation, you can use socket
option TCP_NODELAY, but this is not a portable solution; I think it's a bit
different on Windows than on Linux, and it is not available on all
platforms.  I found no reference to TCP_NODELAY in the Python reference,
which is probably for the best given its non-portability.  If you are
targetting a platform that supports it, you can probably do something like:

  if (os.name == 'nt'):
    TCP_NODELAY = 1
    theSocket.setsockopt(IPPROTO_TCP, TCP_NODELAY, 1)

Depending on the nature of the conversation, one or both sides of your
connection may want to do that.  Protecting the nonstandard NODELAY option
with a platform test means the code will still work on other platforms,
although it will still have a "glass ceiling" on platforms that you haven't
planned for.

Bruce





More information about the Python-list mailing list