錯誤寫法
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 <span class="pre"></span>
<span class="pre"></span>
<span class="pre"></span>
以上是Tornado協程在python2.7是怎麼使用的?的詳細內容。更多資訊請關注PHP中文網其他相關文章!