Tornado协程在python2.7是怎么使用的?


本文摘自php中文网,作者零下一度,侵删。

错误写法

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是怎么使用的?的详细内容,更多文章请关注木庄网络博客!!

相关阅读 >>

Python数字怎么转对应中文

了解Python的collections.counter类型

Python中abs是什么意思

Python3.6和3.7有什么区别

Python怎么安装requests库

如何用Python画一只兔子——turtle库circle()画圆函数的详细用法介绍

Python字典怎么添加元素

初学者学Python看什么书

pandas实现选取特定索引的行

Python实现在两个字典中寻找相同点的方法(附代码)

更多相关阅读请进入《Python》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...