Update to original post: A colleague pointed out what I was doing wrong. I'll give the explanation at the bottom of the post, as it might be helpful for others.
I am trying to get a basic understanding of the limits on network performance of python programs and have run into an anomaly. The code fragment
while 1:
sock.sendto("a",target)
sends UDP packets to a target machine, as fast as the host will send. I measure a sending rate of just over 4000 packets per second, or 250 us per packet. This seems slow, even for an interpreted language like python (the program is running on a 2 GHz AMD opteron, Linux, python version 2.6.6). I've seen much better performance in python for TCP, so I find this a bit weird.
If I run this in the background and run top, I find that python is using just 25% of the cpu, suggesting that python may be artificially delaying the transmission of UDP packets.
Has anyone else experienced anything similar? Does anyone know if python does limit the rate of packet transmission, and if there is a way to turn this off?
BTW, a similar C++ program can send well over 200,000 packets per second, so it's not an intrinsic limit of the platform or OS.
So, it turns out I made a silly newbie mistake. I neglected to call gethostbyname explicitly. Consequently, the target address in the sendto command contained a symbolic name. This was triggering a name resolution every time a packet was sent. After fixing this, I measure a maximum sending rate of about 120,000 p/s. Much better.