Simple threaded web-server based on BaseHTTPServer?

Skip Montanaro skip at pobox.com
Thu Jan 31 12:59:33 EST 2002


    Thomas> Can anybody show me how to create a threaded web-server based on
    Thomas> BaseHTTPServer?

I have a multi-threaded XMLRPC server based on SocketServer.TCPServer.
Using BaseHTTPServer should be similar:

    class MyServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
        ...

    class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
        ...

    server = MyServer(('', port), RequestHandler)
    server.serve_forever()

Each time a request is received, a RequestHandler instance is created to
process it.  This setup works great for me.  Mark Hammond's remarks referred
to problems when you want to create hundreds or thousands of threads.  We
should all have such problems. ;-) In my case, this is beautiful because my
XMLRPC server sits in front of a MySQL database.  It caches search results,
so most of the time response is fast.  Most of the time I have no more than
two or three threads running.  When the server hits a slow SQL query (regex
searches kill MySQL because it can't use indexes to speed them up), that one
query doesn't bring the entire system to a grinding halt as it used to
before I threaded my server.  Of course, it took me a few days to implement
all the data structure locks.

    Thomas> And if anybody has an opinion about it's ok performance wise to
    Thomas> base a Threaded-webserver on BaseHTTPServer or go for Medusa?

If you're out playing with the big trucks, Medusa is the way to go, though
you need to make sure your system isn't compute-bound (or that your
computational tasks are broken up into small enough chunks that allow for
occasional I/O).  If your performance requirements are more pedestrian,
multiple threads will probably work just fine.

    Thomas> What does Medusa offer over the threaded BaseHTTPServer?

Much more efficient response for I/O-bound servers.

-- 
Skip Montanaro (skip at pobox.com - http://www.mojam.com/)




More information about the Python-list mailing list