Problem with writing fast UDP server

Krzysztof Retel Krzysztof.Retel at googlemail.com
Thu Nov 20 10:03:46 EST 2008


Hi guys,

I am struggling writing fast UDP server. It has to handle around 10000
UDP packets per second. I started building that with non blocking
socket and threads. Unfortunately my approach does not work at all.
I wrote a simple case test: client and server. The client sends 2200
packets within 0.137447118759 secs. The tcpdump received 2189 packets,
which is not bad at all.
But the server only handles 700 -- 870 packets, when it is non-
blocking, and only 670 – 700 received with blocking sockets.
The client and the server are working within the same local network
and tcpdump shows pretty correct amount of packets received.

I included a bit of the code of the UDP server.

class PacketReceive(threading.Thread):
    def __init__(self, tname, socket, queue):
        self._tname = tname
        self._socket = socket
        self._queue = queue
        threading.Thread.__init__(self, name=self._tname)

    def run(self):
        print 'Started thread: ', self.getName()
        cnt = 1
        cnt_msgs = 0
        while True:
            try:
                data = self._socket.recv(512)
                msg = data
                cnt_msgs += 1
                total += 1
		# self._queue.put(msg)
                print  'thread: %s, cnt_msgs: %d' % (self.getName(),
cnt_msgs)
            except:
                pass


I was also using Queue, but this didn't help neither.
Any idea what I am doing wrong?

I was reading that Python socket modules was causing some delays with
TCP server. They recomended to set up  socket option for nondelays:
"sock.setsockopt(SOL_TCP, TCP_NODELAY, 1) ". I couldn't find any
similar option for UDP type sockets.
Is there anything I have to change in socket options to make it
working faster?
Why the server can't process all incomming packets? Is there a bug in
the socket layer? btw. I am using Python 2.5 on Ubuntu 8.10.

Cheers
K



More information about the Python-list mailing list