M
M
MonsterAndrew2017-09-18 21:40:18
Python
MonsterAndrew, 2017-09-18 21:40:18

How does async work in Tornado?

There is this code:

import tornado.web
from tornado.ioloop import IOLoop
from tornado import gen
class MainHandler(tornado.web.RequestHandler):
    async def get(self):
        for i in range(5):
            if i == 4:
                print(i)
            await gen.sleep(1)
        print("Lol")
application = tornado.web.Application([
    (r"/test", MainHandler),
    ])
if __name__ == "__main__":
    application.listen(9999)
    IOLoop.instance().start()

I thought that when requested, it would first display "Lol", then count up to 4 and display 4, since async is like await is. What was my surprise when he first counted to 4, printed 4 and only then printed "Lol". I also tried options without async/await but with gen .coroutine. The result is still the same :( I'm new to Tornado, so don't judge too harshly.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question