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解释器

Python可以开发软件吗

Python如何判断字符串类型

Pythonidle怎么打开

Python怎么去重

详细介绍有关Python+socket实现基于tcp协议的客户与服务端中文自动回复聊天功能

linux下修改Python命令的方法示例(附代码)

sqrt是什么函数

Python怎么使用pip工具

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




打赏

取消

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

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

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

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

评论

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