How to use Tornado coroutine in python2.7?

零下一度
Release: 2017-06-23 11:27:19
Original
1697 people have browsed it

Error writing

class RemoteHandler(web.RequestHandler):

    @gen.coroutine
    def get(self):
        response = httpclient('http://www.baidu.com')
        self.write(response.body)

    @gen.coroutine
    def httpClient(url):
        result = yield httpclient.AsyncHTTPClient().fetch(url)
        return result
Copy after login

 

If you follow the general method of return, an error will be reported

You need to use raise gen.Return(response.body) instead of return

Official example

@gen.coroutine
def fetch_json(url):
    response = yield AsyncHTTPClient().fetch(url)
    raise gen.Return(json_decode(response.body))
Copy after login

 

In Python 3.3, this exception is no longer necessary: ​​the <span class="pre">return</span> statement can be used directly to return a value (previously <span class="pre">yield</span> and <span class="pre">return</span> with a value could not be combined in the same function).

In python 3.3 or above, there is no need to throw an exception and you can directly use return to return the value directly. In previous versions, yield and return with a return value could not be in the same function.

The above is the detailed content of How to use Tornado coroutine in python2.7?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template