I am trying to understand how tornado works. While understanding, I tried to implement a simple coroutine example using tornado.gen
. But the example doesn't seem to work. Any leads on what is going wrong here?
import tornado.web
from tornado import gen
class MainHandler(tornado.web.RequestHandler):
@gen.coroutine
def get(self):
print("Handling starts")
yield tornado.gen.sleep(5)
print("Handling ends")
self.write('Hi')
When I open multiple browser tabs and try to request the server, all the requests except the current one gets blocked and tornado seems to handle these requests in a synchronous way:
Handling starts
Handling ends
Handling starts
Handling ends
Handling starts
Handling ends
Handling starts
Handling ends
Handling starts
Handling ends
Handling starts
Handling ends
Browsers don't make multiple requests to the same resource at the same time. For more explanation, see: Chrome stalls when making multiple requests to same resource?
This has also been mentioned in Tornado FAQs.
Anyway, to see the asynchronous behaviour of Tornado, make one request from your browser, and another from your terminal:
curl http://127.0.0.1:8888/
You should see the the output as expected.