473,503 Members | 1,666 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Scatter/gather on sockets?

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?
Apr 1 '06 #1
5 1396
On Sat, 01 Apr 2006 14:56:02 -0500, 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?


Is sendall() what you're looking for?

--
A wise man knows he knows nothing.

Apr 1 '06 #2
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

Apr 1 '06 #3
Peter Hansen <pe***@engcorp.com> wrote:
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()).
This is true, but I'm generating the message being sent in very small
chunks (often as small as 4 bytes at a time), and typically need to flush a
packet out onto the network after a few dozen bytes. Maybe at most a few
hundred. I don't know of any networks with MTU's smaller than that.
Measurements show a 10-fold improvement in protocol throughput with large
packets vs. small ones. The only question is what's the most efficient way
in Python to generate the large packets.
So fixing your design not to require this appears to be the only viable
solution.


My design is not broken. I'm writing code to drive a pre-existing binary
communications protocol. It is what it is. The functionality I seek
exists at the Unix system call level (writev, sendmsg), but doesn't appear
to be exposed in the Python socket API.
Apr 1 '06 #4
In article <pa****************************@zetafunc.net>,
Anthony Greene <sy******@zetafunc.net> wrote:
On Sat, 01 Apr 2006 14:56:02 -0500, 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?


Is sendall() what you're looking for?


No. Sendall() is actually what I'm using now. It handles the other side
of the issue; issuing repeated send() calls if the system fragments your
buffer. I'm trying to aggregate lots of small buffers into one large one.
Apr 1 '06 #5
Roy Smith <ro*@panix.com> writes:
This is true, but I'm generating the message being sent in very small
chunks (often as small as 4 bytes at a time), and typically need to flush a
packet out onto the network after a few dozen bytes. Maybe at most a few
hundred. I don't know of any networks with MTU's smaller than that.
Measurements show a 10-fold improvement in protocol throughput with large
packets vs. small ones. The only question is what's the most efficient way
in Python to generate the large packets.


Probably: build up the packet with cStringIO or with the array module
instead of as a list of small strings. But if you time both versions
I don't think it'll matter much. Python (at least CPython) simply
will not be very fast no matter what you do. The overhead of building
a large string (with ''.join, say) from a bunch of small ones isn't
that big a deal compared with what you already lose in interpreter
overhead running the application.
Apr 1 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
7050
by: rhmd | last post by:
I need to create image files (eg bmp or jpeg) of xy scatter graphs (i.e., graphs in which markers denote individual points; the markers need to be small polygons of various sizes, shapes, colors,...
9
12771
by: Dr. Colombes | last post by:
What is the easiest way to generate some plots and graphs in Python ? Specifically interested in simple histograms and scatter plots with circles and regression lines. Thanks for your...
0
4475
by: P Fu | last post by:
I am trying to plot pair using the XY scatter chart in Access and place it into a report. I want my x-axis to have "Age" values and the y-axis to have "Gross Area" values, both pulled using a...
0
1682
by: Dave | last post by:
I have a scatter chart that's based on a SQL Stored Procedure. Procedure works great, but when I apply it to the chart, the X axis returns a series of numbers that I think are record numbers. The...
3
3467
by: Joe | last post by:
This probably isn't the right group for this kind of question but I figured I would take a shot... I want to remove points that skew a scatter plot. For example 20 data points are around -75 to...
8
30466
by: Derek Basch | last post by:
Can anyone give any suggestions on how to make a logarithmic (base 10) x and y axis (loglog) plot in matplotlib? The scatter function doesn't seem to have any log functionality built into it. ...
2
2523
by: DFS | last post by:
I can't get the Access scatter chart to place the data points correctly, ie at the intersection of their XY position. Is this a bug, or is it me? Thanks
18
2687
by: DFS | last post by:
I posted this question yesterday, and nothing but silence. I looked through cdma in Google, and it seems whenever someone posts questions about scatter charts, they get ignored. So, hopefully,...
4
2007
by: Phil Stanton | last post by:
Is it possible to build a chart where you supply the X & Y Co-ordinates and have the data displayed at that point. To clarify:- Suppose we have a cemetary (Scatter seems inapproporiate here) and...
1
5570
by: kjb034 | last post by:
I have the following macro. It works fine, creating an XY scatter plot in Excel 2003, but when one of my users runs it in Excel 2007, it creates a scatter plot with lines. Why does it do this? I...
0
7087
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7281
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7334
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7462
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3156
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1514
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
737
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.