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
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))
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!