I wanted to create a simple HTTP Server which can hanlde about 1000 TPS and came across the SocketServer module in python. The HTTP server is almost giving the expected transactions per second but it almost using 90% of the CPU all the time which is slowing down the other processes.
I have used TheadingTCPServer from SocketServer Module to create a http server which intern uses ThreadingMixIn to create new thread for each incoming connections.
anyone faced similar issues with SocketServer module in python? Anyway to resolve this?
Sample code i have been using is as below :
import SocketServer
import BaseHTTPServer
class Runner(BaseHTTPServer.BaseHTTPRequestHandler):
def do_POST(self):
try:
req_hdr = self.headers
content_length = req_hdr.getheaders('content-length')
try:
body_length = int(content_length[0])
except:
body_length = 0
req = self.rfile.read(body_length)
if "test" in req:
resp_body = '''<?xml version="1.0" encoding="UTF-8" ?><Status>OK</Status>'''
else:
resp_body = '''<?xml version="1.0" encoding="UTF-8" ?><Status>Not OK</Status>'''
self.send_response(200, 'OK')
self.send_header("Content-Length", len(resp_body))
self.send_header("Content-Type", "text/xml")
self.end_headers()
self.wfile.write(resp_body)
except:
print "exception"
def log_message(self, format, *args):
return
def main():
server_address = ('', 8100)
httpd = SocketServer.ThreadingTCPServer(server_address, Runner)
httpd.serve_forever()
if __name__ == '__main__':
main()