錯誤寫法
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會報錯
需要使用 raise gen.Return(response.body) 代替return
官方例子
@gen.coroutinedef 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).
在python 3.3以上版本, 不在需要拋出異常,可以直接使用return直接返回值。而在之前的版本中,yield和帶有返回值的return不能處于一個函數(shù)當中。
以上這篇Tornado協(xié)程在python2.7如何返回值(實現(xiàn)方法)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持武林站長站。
新聞熱點
疑難解答