찾다

 >  Q&A  >  본문

python - tonado raise gen.Return 错误

最近在使用 Tornado,用到 gen.coroutine 和 yield 配合,但是出了些问题,一直不明白!

代码:

class BaseHandler(tornado.web.RequestHandler):
    @gen.coroutine
    def args_kwargs(self,pro):
        try:
            kwargs = self.get_argument("data",None)
            if kwargs:
                code="-10000"
                raise gen.Return(code)
        except:
            print traceback.format_exc()

class EventAPIHandler(BaseHandler):
    @gen.coroutine
    def post(self):
        try:
            code = yield self.args_kwargs("event")
            if code:
                self.write(re_code[code])
                self.finish()
        except Exception,e:
            print traceback.format_exc()

错误为:

Traceback (most recent call last):
  File "server.py", line 124, in args_kwargs
    raise gen.Return(code)
Return

不能返回数据,请问有大神知道原因吗?请指教,非常感谢!

PHP中文网PHP中文网2888일 전336

모든 응답(2)나는 대답할 것이다

  • 伊谢尔伦

    伊谢尔伦2017-04-17 17:32:03

    원본에서 try Except를 제거하세요. try Except가 발생하면 catch됩니다. 다음과 같이 변경하면 작동합니다

    으아악

    회신하다
    0
  • 阿神

    阿神2017-04-17 17:32:03

    Python2.x에서는 생성기가 직접 return 值할 수 없으므로 Tornado는 값을 특수 예외로 래핑하여 반환합니다. 따라서 try..Exception은 이 예외를 포착하고 오류는 다음과 같습니다. 보고되었으므로 gen.Return의 코드를 변경하십시오. BaseHandler.args_kwargsBTW에는
    만 사용할 필요는 없는 것 같습니다. > 그렇죠? args_kwargs 으아아아

    회신하다
    0
  • 취소회신하다