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不能处于一个函数当中。
相关推荐
天高任鸟飞 2020-06-04
83510998 2020-07-18
thickbookszone 2020-06-14
xiaonamylove 2020-06-14
Magicsoftware 2020-06-01
Reiki 2020-05-04
小菜鸟的代码世界 2020-05-04
whynotgonow 2020-04-26
zcyuefan 2020-04-19
神龙 2020-02-10
ajaxtony 2020-02-03
HongKongPython 2020-01-30
GoatSucker 2020-01-24
liuyang000 2020-01-24
zhiyuan0 2020-01-06
狼窝 2019-12-12