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+selenium开发环境教程讲解

Python怎么生成时间戳

实例详解Python基于回溯法子集树模板解决最佳作业调度

Python中关于str与repr的使用详解

Python中if语句用法

Python删除list中的重复元素

Python是强类型语言吗

Python中的seed()方法怎么用

Python如何将客户的数据一直保存

Python for语句的执行过程是什么

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




打赏

取消

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

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

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

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

评论

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