Tornado協(xié)程在python2.7如何返回值(實(shí)現(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ù)當(dāng)中。
以上這篇Tornado協(xié)程在python2.7如何返回值(實(shí)現(xiàn)方法)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
基于python全局設(shè)置id 自動化測試元素定位過程解析
這篇文章主要介紹了基于python全局設(shè)置id 自動化測試元素定位過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-09-09python實(shí)現(xiàn)在windows下操作word的方法
這篇文章主要介紹了python實(shí)現(xiàn)在windows下操作word的方法,涉及Python操作word實(shí)現(xiàn)打開、插入、轉(zhuǎn)換、打印等操作的相關(guān)技巧,非常具有實(shí)用價值,需要的朋友可以參考下2015-04-04