views:

231

answers:

1

I created a threaded socket listener that stores newly accepted connections in a queue. The socket threads then read from the queue and respond. For some reason, when doing benchmarking with 'ab' (apache benchmark) using a concurrency of 2 or more, I always get a connection reset before it's able to complete the benchmark (this is taking place locally, so there's no external connection issue).

class server:    
_ip = ''
_port = 8888

def __init__(self, ip=None, port=None):
    if ip is not None:
        self._ip    = ip
    if port is not None:
        self._port  = port
    self.server_listener(self._ip, self._port)

def now(self):
    return time.ctime(time.time())

def http_responder(self, conn, addr):
    httpobj = http_builder()
    httpobj.header('HTTP/1.1 200 OK')
    httpobj.header('Content-Type: text/html; charset=UTF-8')
    httpobj.header('Connection: close')
    httpobj.body("Everything looks good")        
    data = httpobj.generate()

    sent = conn.sendall(data)


def http_thread(self, id):        
    self.log("THREAD %d: Starting Up..." % id)

    while True: 
        conn, addr = self.q.get()
        ip, port = addr
        self.log("THREAD %d: responding to request: %s:%s - %s" % (id, ip, port, self.now()))
        self.http_responder(conn, addr)                
        self.q.task_done()
        conn.close()

def server_listener(self, host, port):
    self.q = Queue.Queue(0)

    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.bind( (host, port) )
    sock.listen(5)


    for i in xrange(4): #thread count
        thread.start_new(self.http_thread, (i+1, ))

    while True:
        self.q.put(sock.accept())

    sock.close()

server('', 9999)

When running the benchmark, I get totally random numbers of good requests before it errors out, usually between 4 and 500.

Edit: Took me a while to figure it out, but the problem was in sock.listen(5). Because I was using apache benchmark with a higher concurrency (5 and up) it was causing the backlog of connections to pile up, at which point the connections started getting dropped by the socket.

A: 

Is there a reason why you're not using SocketServer which comes with Python? This would handle the situation a lot better. If you're looking to do HTTP stuff, the BaseHTTPServer also provides a framework for this.

Lee
BaseHTTPServer is not threaded and SocketServer does not give you the option to do things like thread pooling.
Ian
You can use SocketServer.ThreadingTcpServer to write a multithreaded HTTP server. For an example see: http://stackoverflow.com/questions/2274320/socketserver-threadingtcpserver-cannot-bind-to-address-after-program-restart
Justin Ethier