首页 > 代码库 > Tornado协程在python2.7如何返回值
Tornado协程在python2.7如何返回值
错误写法
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.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).
在python 3.3以上版本, 不在需要抛出异常,可以直接使用return直接返回值。而在之前的版本中,yield和带有返回值的return不能处于一个函数当中。
Tornado协程在python2.7如何返回值
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。