alauri
alauri

Reputation: 317

Asynchronous operation in Tornado

I'm trying to understand how Tornado works in asynchronous and not-blocking mode.

I just want my RequestHandler writes back to client the result of sum(range(10000000)).

I tried this way inside my RequestHandler:

@tornado.gen.coroutine
def post(self, *args, **kwargs):
    res = yield tornado.gen.Task(self._sum, 10000000)

    self.write("Result: %d" % res)
    self.finish()

def _sum(self, size, callback):
    callback(sum(range(size)))

I'm using the apache2 benchmark tool to test my Tornado with a set of 100 requests and 50 of them concurrently.

The problem is I get the same amount of time of the below code (~15 secs for the whole test):

def post(self, *args, **kwargs):
    res = sum(range(10000000))
    self.write("Result: %d" % res)

I don't understand where I'm doing wrong.

Thank you in advance

Upvotes: 1

Views: 72

Answers (1)

A. Jesse Jiryu Davis
A. Jesse Jiryu Davis

Reputation: 24009

Tornado's not optimized for concurrent calculation - indeed, the standard Python interpreter itself cannot do concurrent calculation in a single process. Tornado is optimized to do asynchronous network operations concurrently. Your test is computationally intensive so you're finding, correctly, that there's no benefit to executing a "yield" around a computation.

Upvotes: 2

Related Questions