首頁 >後端開發 >Python教學 >Tornado協程在python2.7是怎麼使用的?

Tornado協程在python2.7是怎麼使用的?

零下一度
零下一度原創
2017-06-23 11:27:191774瀏覽

錯誤寫法

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>

################################################################################################################################################ #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不能處於一個函數當中。 ###

以上是Tornado協程在python2.7是怎麼使用的?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn