龙卷风@ web.asynchronous @ gen.coroutine VS @ gen.corutine和有什么不一样 [英] tornado what is the difference between @web.asynchronous @gen.coroutine VS @gen.corutine
问题描述
在文档中,如果该方法也用@ gen.coroutine装饰,则@ web.asynchronous是不必要的.像这样
In the document, @web.asynchronous is unnecessary if the method is also decorated with @gen.coroutine. like this
@web.asynchronous
@gen.coroutine
def get(self):
...
但是,在文档中,他们还解释说,如果您使用@ web.asynchronous,则应调用self.finish().但是,在上述情况下(一起使用两个装饰器),无需调用"self.finish()"即可完成连接.
but, In document, They also explain that If you use @web.asynchronous, then you should call self.finish(). However, In above case(using two decorator together) the connection is finished with out calling "self.finish()"
我想知道那里发生了什么.
I'm wondering what happened in there.
并且在下面的情况下,它与上面的工作原理不同.
and In below case, It works different with above.
@web.asynchronous
def get(self):
self.test()
@gen.coroutine
def test(self):
httpClient = AsyncHttpClient()
val = yield httpClient.fetch("http://www.google.com")
print test
#self.finish()
如果未调用"self.finish()",则连接不会关闭.
If "self.finish()" is not called the connection is not closed.
有人可以解释吗?
推荐答案
秘密是此处:
if isinstance(result, Future):
# If @asynchronous is used with @gen.coroutine, (but
# not @gen.engine), we can automatically finish the
# request when the future resolves. Additionally,
# the Future will swallow any exceptions so we need
# to throw them back out to the stack context to finish
# the request.
def future_complete(f):
f.result()
if not self._finished:
self.finish()
IOLoop.current().add_future(result, future_complete)
@asychronous
检查返回未来的方法(即@gen.coroutine
),如果是,则在未来完成时添加IOLoop回调以完成连接.
@asychronous
checks for the method returning a future (i.e. @gen.coroutine
) and if so, adds an IOLoop callback to finish the connection when the future completes.
这篇关于龙卷风@ web.asynchronous @ gen.coroutine VS @ gen.corutine和有什么不一样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!