码迷,mamicode.com
首页 > 编程语言 > 详细

Tornado协程在python2.7如何返回值

时间:2017-06-21 18:16:20      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:blog   ret   response   combine   写法   his   necessary   返回值   doc   

错误写法

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不能处于一个函数当中。

Tornado协程在python2.7如何返回值

标签:blog   ret   response   combine   写法   his   necessary   返回值   doc   

原文地址:http://www.cnblogs.com/big-lazy-cat/p/7060871.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!