2

I'm trying to set up a very simple UDP socket server with python that accepts asynchronous connections. Now I have never used Python much before but I like the language a lot from what I've read about it, which is why I am using it for this test.

I am following the example given here:

http://docs.python.org/library/socketserver.html#asynchronous-mixins

I already have it successfully running as a UDP server (the example is TCP), with very few modifications so it's almost exactly the same as in that example. The only thing I don't get is that it keeps creating new threads for every connection.

Now I may just be miss understanding the concept of threads, which is why I'm asking my question here, but shouldn't it clear up unused threads and re-use them? Or is it already doing that and it's simply an incremental counter indicating the number that I shouldn't care too much about?

So basically, with the given example, can I safely run this (as a test) for hours at an end, with thousands of connections (not concurrently, but over time) without any problems or will it start thousands of threads that linger on for far longer than they should?

Thanks

1 Answer 1

6

No need for threads. Use real asynchronous IO. It scales a lot better. Threads to wait for IO just add overhead, no performance gain.

from twisted.internet.protocol import DatagramProtocol
from twisted.internet import reactor

class Echo(DatagramProtocol):

    def datagramReceived(self, data, (host, port)):
        print "received %r from %s:%d" % (data, host, port)
        self.transport.write(data, (host, port))

reactor.listenUDP(9999, Echo())
reactor.run()
Sign up to request clarification or add additional context in comments.

6 Comments

in the example he gave, the threads are to simulat a client not on the server so scalibility of the server is not related.
@hhafez: huh, no, the example uses thread-per-request approach on the server.
Thanks nosklo, that looks a lot simpler as well. I assume that with your method I don't have to worry about lingering threads? Thanks!
@Nathan: Exactly! Not using threads reduces a lot of issues - you don't have to worry anymore with locks (and deadlocks), your code becomes predicable, debuggable...
@Noctis Skytower: pyzmq can do asynchronous IO in Python 3--whether it will do what you're looking for, I don't know.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.