what does @tornado.web.asynchronous decorator mean

2020-02-19 07:21发布

  1. If code didn't use this decorator, is it non-blocking?
  2. Why this name is asynchronous, it means add decorator let code asynchronous?
  3. Why @tornado.gen always use with @tornado.web.asynchronous together?

3条回答
劳资没心,怎么记你
2楼-- · 2020-02-19 07:26
  1. Answered here: asynchronous vs non-blocking

  2. Think of it like this. When you need to make a request to say a database or another url to retrieve data you do not want to block your tornado IO. So the @tornado.web.asynchronous will allow the IO to handle other requests while it waits for the content to load (ex. database or url).

  3. They are simular. You most likely will use @tornado.web.asynchronous.

查看更多
Anthone
3楼-- · 2020-02-19 07:29

@tornado.web.asynchronous is essentially a just a marker you put on a handler method like get() or post() that tells the framework that it shouldn't call finish() automatically when the method returns, because it contains code that is going to set up finish() to be called at a later time.

查看更多
倾城 Initia
4楼-- · 2020-02-19 07:30

@tornado.web.asynchronous prevents the the RequestHandler from automatically calling self.finish(). That's it; it just means Tornado will keep the connection open until you manually call self.finish().

  1. Code not using this decorator can block, or not. Using the decorator doesn't change that in any way.

  2. As @Steve Peak said, you use the decorator for asynchronous requests, e.g. database retrieval.

  3. Updated for Tornado 3.1+: If you use @gen.coroutine, you don't need to use @asynchronous as well. The older @gen.engine interface still requires @asynchronous, I believe.

查看更多
登录 后发表回答