1

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()
Jerry
  • 60
  • 1
  • 5
  • What does your request handler do? Did you try to run it with an empty handler to see the server's overhead? – 9000 Apr 19 '17 at 15:38
  • without a minimal example it is difficult the point you to your mistake. – Daniel Apr 19 '17 at 15:44
  • @9000 It doesn't do much work, it simply sends a success / failure response based on the incoming request info. – Jerry Apr 19 '17 at 15:45
  • And what kind of CPU is this? A small ARM chip is easier to max out than a large x64. – 9000 Apr 19 '17 at 15:53
  • @Daniel. i have updated the code i have been using. ( minor changes on the response message though ). – Jerry Apr 19 '17 at 16:17
  • Threads are CPU expensive. Creating 1000 threads per seconds could lead to such CPU usage. Use asyncio, select, greenlets or similar. – Daniel Apr 19 '17 at 17:24

0 Answers0