简体   繁体   English

@ tornado.web.asynchronous decorator是什么意思?

[英]what does @tornado.web.asynchronous decorator mean?

  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? 为什么@ tornado.gen总是和@ tornado.web.asynchronous一起使用?

@tornado.web.asynchronous prevents the the RequestHandler from automatically calling self.finish() . @tornado.web.asynchronous 阻止RequestHandler自动调用self.finish() That's it; 而已; it just means Tornado will keep the connection open until you manually call self.finish() . 它只是意味着Tornado将保持连接打开,直到你手动调用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, eg database retrieval. 正如@Steve Peak所说,您使用装饰器进行异步请求,例如数据库检索。

  3. Updated for Tornado 3.1+: If you use @gen.coroutine , you don't need to use @asynchronous as well. 更新了Tornado 3.1+:如果你使用@gen.coroutine ,你也不需要使用@asynchronous The older @gen.engine interface still requires @asynchronous , I believe. 我相信,较旧的@gen.engine接口仍然需要@asynchronous

  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. 当您需要请求说出数据库或其他URL来检索数据时,您不希望阻止龙卷风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). 所以@tornado.web.asynchronous将允许IO在等待加载内容时处理其他请求(例如数据库或url)。

  3. They are simular. 它们是相似的。 You most likely will use @tornado.web.asynchronous . 您很可能会使用@tornado.web.asynchronous

@tornado.web.asynchronous本质上只是一个标记,你放在一个像get()post()这样的处理程序方法上,告诉框架它不应该在方法返回时自动调用finish() ,因为它包含的代码是将要设置finish()以便稍后调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM