How does the Tornado coroutine return the value of in python2.7

  • 2020-06-03 07:19:15
  • OfStack

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

return will report an error in a 1-like way

raise ES11en. Return(ES13en. body) is needed instead of return

The 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 return statement can be used directly to return a value (previously yield and return with a value could not be combined in the same function).

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


Related articles: