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

相关阅读 >>

js中sqlalchemy的实例详解

Python装饰器详细介绍

Python怎么表示八进制

Python关于变量赋值的秘密介绍

Python中def是什么意思

在jupyter notebook中同时安装Python2和Python3

如何将字符串转换为datetime

Python可以写病毒吗

Python使用pylab库实现绘制直方图功能

Python中randint函数的用法是什么?

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




打赏

取消

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

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

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

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

评论

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

    暂无评论...