Scatter/gather on sockets?

Peter Hansen peter at engcorp.com
Sat Apr 1 15:21:08 EST 2006


Roy Smith wrote:
> I've got a bunch of strings in a list:
> 
> vector = []
> vector.append ("foo")
> vector.append ("bar")
> vector.append ("baz")
> 
> I want to send all of them out a socket in a single send() call, so
> they end up in a single packet (assuming the MTU is large enough).  I
> can do:
> 
> mySocket.send ("".join (vector))
> 
> but that involves creating an intermediate string.  Is there a more
> efficient way, that doesn't involve that extra data copy?

Two possible answers that I can see:

A. No, the send call is implemented in C and requires a single buffer 
with the entire piece of data that will be sent.  Ultimately this gets 
passed down to the NIC hardware in some fashion, so there's certainly no 
hope of using something like a generator to send it in pieces.

B. Don't bother trying, because even if the MTU is large enough there is 
absolutely no guarantee that the packet will stay intact all the way 
through the network anyway (even if you use sendall() instead of send()).

So fixing your design not to require this appears to be the only viable 
solution.

-Peter




More information about the Python-list mailing list