我是靠谱客的博主 专注花瓣,这篇文章主要介绍Tornado协程在python2.7如何返回值(实现方法),现在分享给大家,希望可以做个参考。

错误写法

复制代码
1
2
3
4
5
6
7
8
9
10
11
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

官方例子

复制代码
1
2
3
4
@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如何返回值(实现方法)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

最后

以上就是专注花瓣最近收集整理的关于Tornado协程在python2.7如何返回值(实现方法)的全部内容,更多相关Tornado协程在python2内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(121)

评论列表共有 0 条评论

立即
投稿
返回
顶部