3

我收到此错误:

HTTP 599:连接已关闭 [E 130405 11:43:14 web:1031] 未捕获的异常 GET /networks/1/sensors/1/alarm (127.0.0.1)

在执行以下代码时:

from tornado.stack_context import ExceptionStackContext

def handle_exc(*args):
print('Exception occured')
return True

@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 is not None:
            print("Terzo")
            print response.error
            with ExceptionStackContext(handle_exc):
                response.rethrow()
    else:
        print('Handle request')

    for line in response.body.split("\n"):
                if line != "": 
                    #net = int(line.split(" ")[1])
                    #sens = int(line.split(" ")[2])
                    #stype = int(line.split(" ")[3])
                    value = int(line.split(" ")[4])
                    print value
                    yield value
                    return


class AlarmHandler(BaseHandler):
    @tornado.web.authenticated
    @tornado.web.asynchronous
    @tornado.gen.engine
    def get(self, netid, sensid):
        self.lock_tables("read", ['devices'])
        status = self.db.get("SELECT status from devices \
                          WHERE id=%s AND network_id=%s", sensid, netid)
        print("Primo")
        print status

        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
        if value != status['status']:

            self.lock_tables("write", ['devices'])
            self.db.execute("UPDATE devices SET status=%s \
                             WHERE id=%s AND network_id=%s", value, netid, sensid)
            self.unlock_tables()
            self.notice("Status changed")

在课堂AlarmHandler上有一个名为check_status_changes. 在这个函数中,当有 response.error 时,我会获取标题的错误。

如何设置 if 错误条件以返回课堂并管理情况?谢谢你。

其他信息

如果我做一个 Tornado 屏幕,我会看到:

普里莫

{'状态':无}

没有任何

泰尔佐

HTTP 599:连接关闭

发生异常

所以,我认为程序在异常是rethtow之前关闭了连接!

在 html 控制台中,我看到了这个错误:

文件“./wsn.py”,第 226 行,在 response.body.split("\n") 中的 check_status_changes 行:AttributeError: 'NoneType' object has no attribute 'split'

4

1 回答 1

0

你提出了一个例外:

if response.error:
        print("Terzo")
        print response.error
        raise Exception(response.error)
        return

这将是一个Uncaught Exception. 您应该对此进行编码以处理异常而不引发异常。例如,记录错误消息并中止数据库更新。

于 2013-04-05T12:43:56.033 回答