源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

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

  • 时间:2022-06-23 05:07 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Tornado协程在python2.7如何返回值(实现方法)
[b]错误写法[/b]
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会报错 需要使用[b]raise gen.Return(response.body) 代替return[/b] [b]官方例子[/b]
@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如何返回值(实现方法)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持编程素材网。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部