錯(cuò)誤寫法
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會(huì)報(bào)錯(cuò)
需要使用 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不能處于一個(gè)函數(shù)當(dāng)中。
以上這篇Tornado協(xié)程在python2.7如何返回值(實(shí)現(xiàn)方法)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持VEVB武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選