Python BadyElderError:生成未知对象HTTPError(';HTTP 599:连接已关闭';,)

Python BadyElderError:生成未知对象HTTPError(';HTTP 599:连接已关闭';,),python,return,tornado,yield,Python,Return,Tornado,Yield,我想知道为什么在这个函数中: @tornado.gen.engine def check_status_changes(netid, sensid): como_url = "".join(['http://131.114.52:44444/ztc?netid=', str(netid), \ '&sensid=', str(sensid), '&start=-5s&end=-1s']) http_client = Asy

我想知道为什么在这个函数中:

@tornado.gen.engine
def check_status_changes(netid, sensid):

    como_url = "".join(['http://131.114.52:44444/ztc?netid=', str(netid), \
               '&sensid=', str(sensid), '&start=-5s&end=-1s'])

    http_client = AsyncHTTPClient()
    response = yield tornado.gen.Task(http_client.fetch, como_url)

    if response.error:
        raise Exception(response.error)
当出现response.error时,我获取标题错误。。。为了在另一个函数中捕获返回值,我必须生成什么

之后我会做一些类似的事情:

try:
        periodic = tornado.ioloop.PeriodicCallback(check_status_changes(netid, sensid), 5000)
        value = periodic.start()
        print("Secondo")
        print value
    except:
        print("Quarto")
        periodic.stop()
        self.finish()
        return
    else:
我不知道。。。我只是将返回值与另一个值进行比较


谢谢。

该函数有一个
gen.engine
decorator,不能从内部返回值(与龙卷风无关,不能在生成器内部返回值)

如果您试图从该函数中获取一个值(假设您在IOLoop上调用该函数),则该函数应该有一个
回调
(可调用)关键字参数:

@tornado.gen.engine
def check_status_changes(netid, sensid, callback=None):
    response = yield tornado.gen.Task(do_your_thing)
    if response.error:
        raise response.error
    callback(response.body)  # this is how you make the value available to the
                             # caller; response.body is HTTPResponse specific iirc
现在,您可以在其他地方调用此函数:

# somewhere in a gen.engine decorated async method
body = yield tornado.gen.Task(check_status_changes, netid, sensid)
你可以用

raise tornado.gen.Return(response)

为什么不提出一个例外呢?然后在此生成器的使用者中单独处理异常。我该怎么做?我不能提出一个坏的收益率异常…创建你自己的异常?你可以引发任何你喜欢的异常。引发异常(response.error)@RickyA:这是tornado基于生成器的异步引擎。是的,但是如果你看到代码的第二部分,我会在定期回调中调用check\u status\u changes。在这种情况下我该怎么办?你的解决方案很好。定期打电话!谢谢
    class MainHandler(tornado.web.RequestHandler):
        @gen.coroutine
        def get(self):
            http_client = AsyncHTTPClient()
            http_client = tornado.httpclient.AsyncHTTPClient()
            response = yield http_client.fetch('http://localhost:1338/api/getDistinctGeoPositions/?durationInMinutes=9000')
            if response.error:
                print response.error
            self.finish(response.body)