关于tornado的raise gen.Retuen()

raise gen.Return(response.body)在python3.3以后作用相当于return, 在python3.3之前作用是返回一个异常值, 和返回一个value

python 3.3之后直接使用return

回调函数处理异步

from tornado.httpclient import AsyncHTTPClient

def asynchronous_fetch(url, callback):
    http_client = AsyncHTTPClient()
    def handle_response(response):
        callback(response.body)
    http_client.fetch(url, callback=handle_response)

协程处理异步

from tornado import gen

@gen.coroutine
def fetch_coroutine(url):
    http_client = AsyncHTTPClient()
    response = yield http_client.fetch(url)
    raise gen.Return(response.body)

posted @ 2019-10-09 15:33  lvph  阅读(688)  评论(0编辑  收藏  举报