Tornado, what is the difference between @ web.asynchronous @ gen.coroutine VS @ gen.corutine

In the document , @ web.asynchronous is not required if the method is also decorated with @ gen.coroutine. like this

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

but in the document they also explain that if you use @ web.asynchronous, then you should call self.finish (). However, in the above case (using two decorators together), the connection ends without calling "self.finish ()"

I wonder what happened there.

and in the following case, it differs from the previous one.

@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()

If "self.finish ()" is not called, the connection does not close.

Can this be explained?

+4
source share
1 answer

:

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 , .

+8

Source: https://habr.com/ru/post/1535774/


All Articles