I have a python script that is a http-server: http://paste2.org/p/89701, when benchmarking it against ApacheBench (ab) with a concurrency level (-c switch) that is lower then or equal to the value i specified in the socket.listen()-call in the sourcecode everything works fine, but as soon as put the concurrency level in apache bench above the value in the socket.listen()-call performance drops through the floor, some example:
Nothing changes in the code between the two calls, I can’t figure out what is wrong - been at this problem for one day now. Also note that: The multiplexing version of the same code (I wrote to compare to the threaded version) works FINE no matter what socket.listen() is set to or what the concurrency (-c switch) in apache is set to.
I've spent a day on IRC/python docs, posted on comp.lang.python and on my blog - I can't find ANYONE that even has an idea what could be wrong. Help me!
For the heck of it I also implemented an asynchronous version:
import socket, Queue, select
class Request(object):
def __init__(self, conn):
self.conn = conn
self.fileno = conn.fileno
self.perform = self._perform().next
def _perform(self):
data = self.conn.recv(4048)
while '\r\n\r\n' not in data:
msg = self.conn.recv(4048)
if msg:
data += msg
yield
else:
break
reading.remove(self)
writing.append(self)
data = 'HTTP/1.1 200 OK\r\n\r\nHello World'
while data:
sent = self.conn.send(data)
data = data[sent:]
yield
writing.remove(self)
self.conn.close()
class Acceptor:
def __init__(self):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(('', 1234))
sock.listen(10)
self.sock = sock
self.fileno = sock.fileno
def perform(self):
conn, addr = self.sock.accept()
reading.append(Request(conn))
if __name__ == '__main__':
reading = [Acceptor()]
writing = list()
while 1:
readable, writable, error = select.select(reading, writing, [])
for action in readable + writable:
try: action.perform()
except StopIteration: pass
which performs:
ab -n 10000 -c 10 http://127.0.0.1:1234/ --> 16822.13 [#/sec]
ab -n 10000 -c 11 http://127.0.0.1:1234/ --> 15704.41 [#/sec]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With