lundi 18 janvier 2016

Tornado take full CPU usage and concurrent is not acceptable

Code is very simple, this basic usage of Tornado:

class JustHandler(tornado.web.RequestHandler):
    def get(self):
        self.write('just get.')

application = tornado.web.Application([
    (r"/just", JustHandler),
])
application.listen(7777)
tornado.ioloop.IOLoop.current().start()

And I wrote a simple client to send requests:

class Request:
    def GetEmpty(self):
        try:
            req  = urllib2.Request('http://localhost:7777/just')
            resp = urllib2.urlopen(req, timeout = 15)
            file = resp.read()
            print 'Get empty'
        except Exception,e:
            print str(e)

def ReqThread():
    req = Request()
    while True:
        req.GetEmpty()

if __name__ == "__main__":
    print 'start.'
    threads = []
    for i in range(0, 100):
        thread = threading.Thread(target = ReqThread)
        thread.start()
        threads.append(thread)

    for thread in threads:
        thread.join()
    print 'end.'

As you see, I open 100 threads, each thread with loop request without end, then check CPU usage of tornado, it goes 100%.

If I try to increase threads count, some request will fail with timeout.

What am I wrong? Tornado should accept high requsets at a same time.

My system is Ubuntu 14.04, Tornado version is 4.2.1, CPU is E3 1231 v3.




Aucun commentaire:

Enregistrer un commentaire