【问题标题】:tornado what is the difference between @web.asynchronous @gen.coroutine VS @gen.corutine龙卷风 @web.asynchronous @gen.coroutine VS @gen.coroutine 有什么区别
【发布时间】:2014-04-09 16:01:28
【问题描述】:

document 中,如果方法也用@gen.coroutine 修饰,则@web.asynchronous 是不必要的。像这样

@web.asynchronous
@gen.coroutine
def get(self):
    ...

但是,在文档中,他们还解释说,如果您使用 @web.asynchronous,那么您应该调用 self.finish()。但是,在上述情况下(同时使用两个装饰器)连接完成,无需调用“self.finish()”

我想知道那里发生了什么。

在下面的情况下,它与上面的工作方式不同。

@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()”,则连接不会关闭。

有人能解释一下吗?

【问题讨论】:

    标签: asynchronous tornado


    【解决方案1】:

    秘密是here:

    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 回调以在未来完成时完成连接。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-27
      • 2016-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-15
      • 1970-01-01
      • 2015-02-08
      相关资源
      最近更新 更多