您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Tornado协程在python2.7如何返回值(实现方法)

5b51 2022/1/14 8:17:00 python 字数 2402 阅读 340 来源 www.jb51.cc/python

错误写法 classRemoteHandler(web.RequestHandler): @gen.coroutine defget(self): response=httpclient(\'http://www.baidu.com\')

概述

错误写法

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

官方例子

@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.7如何返回值(实现方法)所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶