Tornado協(xié)程在python2.7如何返回值(實現(xiàn)方法)
錯誤寫法
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不能處于一個函數(shù)當中。
以上這篇Tornado協(xié)程在python2.7如何返回值(實現(xiàn)方法)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
python實現(xiàn)在windows下操作word的方法
這篇文章主要介紹了python實現(xiàn)在windows下操作word的方法,涉及Python操作word實現(xiàn)打開、插入、轉換、打印等操作的相關技巧,非常具有實用價值,需要的朋友可以參考下2015-04-04