codersofthedark
codersofthedark

Reputation: 9665

How to optimize Tornado?

The following code fetches parameter from request and respond from couchbase db as per the value of the parameter.

couchbase = Couchbase("ubuntumartini03:8091", "thebucket", "")
bucket = couchbase["thebucket"]

class MH(tornado.web.RequestHandler):
        def get(self):
                key = self.get_argument("pub_id", strip=True)
                result = json.loads(bucket.get(key)[2])
                self.write(result['metaTag'])


if __name__=="__main__":
        app = tornado.web.Application(handlers=[(r"/", MH)])
        app.listen(8888,"")
        tornado.ioloop.IOLoop.instance().start()

Problem: For the given hardware, we can make 10k/sec calls to Couchbase from Tornado machine. But when we are making a call from client to Tornado machine, we are only able to make 350 calls/sec.

Surely the bottleneck here is Tornado. How to optimize it to be able to make atleast 7k calls/sec?

Upvotes: 4

Views: 713

Answers (2)

DachuanZhao
DachuanZhao

Reputation: 1349

Edit your code like this :

from tornado.ioloop import IOLoop


couchbase = Couchbase("ubuntumartini03:8091", "thebucket", "")
bucket = couchbase["thebucket"]

class MH(tornado.web.RequestHandler):
    async def get(self):
        key = self.get_argument("pub_id", strip=True)
        result = await IOLoop.current().run_in_executor(None,bucket.get,*(key))
        self.write(result[2]['metaTag'])


if __name__=="__main__":
    app = tornado.web.Application(handlers=[(r"/", MH)])
    app.listen(8888,"")
    tornado.ioloop.IOLoop.instance().start()

Upvotes: 1

Yarkee
Yarkee

Reputation: 9424

What client do you use, is it a synchronous or an ansynchronous client? If this is a synchronous client, it can't not make full use of the tornado ioloop reactor.

Upvotes: 0

Related Questions