0

大家好,我在 Tornado 有这个主要功能:

def main():
    tornado.options.parse_command_line()
    http_server = tornado.httpserver.HTTPServer(Application())
    http_server.listen(options.port)
    periodic = tornado.ioloop.PeriodicCallback(check_commands_response, 5000)
    periodic.start()
    tornado.ioloop.IOLoop.instance().start()

现在,你可以看到,我在 main 中放入了一个调用函数的周期性函数。

在这个函数中,我会使用某些类中的一些函数方法......所以我不能把这个 check_commands_response 放在类之外。但我不能将函数也放入类中(它是一个 BaseHandler),因为当 main 启动时,函数还没有定义......

我能怎么做?

编辑

如果我写这个有什么问题:

class CheckCommandResponse(BaseHandler):
    @tornado.web.authenticated
    @tornado.web.asynchronous
    @tornado.gen.engine

    @staticmethod
    def check_commands_response(self):
        self.lock_tables("read", ['networks'])
        nets = self.db.query("SELECT DISTINCT netid from networks")
        self.unlock_tables

        for net in nets:
            got_commands_response(net)

    @staticmethod
    def got_commands_response(netid):
        como_url = "".join("http://xx.xx.xx.xx:44444/ztc_config?netid=" \
                   + netid + "&opcode_group=0&opcode=0&start=-10s&end=-1s")

        http_client = AsyncHTTPClient()
        #asynchronous alternative to time.sleep
        yield tornado.gen.Task(tornado.ioloop.IOLoop.instance().add_timeout, time.time() + 5)
        response = yield tornado.gen.Task(http_client.fetch, como_url)

        print response




################################################################################
# Application Entry Point
################################################################################
def main():
    tornado.options.parse_command_line()
    http_server = tornado.httpserver.HTTPServer(Application())
    http_server.listen(options.port)
    periodic = tornado.ioloop.PeriodicCallback(CheckCommandResponse.check_commands_response, 5000)
    periodic.start()
    tornado.ioloop.IOLoop.instance().start()
4

0 回答 0