[英]what does @tornado.web.asynchronous decorator mean?
@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()
。
Code not using this decorator can block, or not. 不使用此装饰器的代码可以阻止或不阻止。 Using the decorator doesn't change that in any way.
使用装饰器不会以任何方式改变它。
As @Steve Peak said, you use the decorator for asynchronous requests, eg database retrieval. 正如@Steve Peak所说,您使用装饰器进行异步请求,例如数据库检索。
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
。
Answered here: asynchronous vs non-blocking 这里回答: 异步与非阻塞
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)。
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.