def _release_chanel_handler(self):
        """
        释放超时消息

        :return:
        """
        current_time = time.time()
        loop_count = 1

        for receive_id in self.sub_dicts.keys():
            (chanel_id, start_time, handler) = self.sub_dicts[receive_id]
            diff_time = current_time - start_time
            loop_count += 1
            if loop_count > self.loop_count:
                logger.warn("loop count exceed %d for _release_chanel_handler", self.loop_count)
                break

            #
            # 连接已经关闭
            #
            if handler == None or handler.request.connection.stream._closed == True:
                logger.info("remove closed sub chanel id:%s", chanel_id)
                self.unsubscribe(receive_id)
                continue
            if diff_time > self.pubsub_timeout:
                self.unsubscribe(receive_id)
                continue

            pass
示例#2
0
def main():
    #
    # 首先解析默认参数
    #
    parse_command_line()

    config_path = options.config
    if not config_path:
        config_path = os.path.join(os.path.dirname(__file__), "pubsub.ini")

    try:
        #
        # 读取配置文件
        #
        config.read(config_path)
    except:
        logger.warning("Invalid config path:%s", config_path)
        return

    root_path = os.path.dirname(os.path.abspath(__file__))
    root_path = os.path.join(root_path, "cache")
    #
    # settings
    #
    settings = {
        'debug'         : False,  # True允许自加载,便于运行中修改代码
        'static_path'   : os.path.join(os.path.dirname(__file__), "www", "statics"),
        'template_path' : os.path.join(os.path.dirname(__file__), "www", "templates"),
        'root_path'     : root_path,
        "xheaders"      : True,
    }
    handlers = [
        (r"/(favicon\.ico)", tornado.web.StaticFileHandler, dict(path=settings['static_path'])),
        (r"/statics/(.*)/?", tornado.web.StaticFileHandler, dict(path=settings['static_path'])),
        ]

    tornado_app = SEApplication(
        handlers=handlers,
        **settings
        )

    logger.info("begin to start server on port:%d", options.port)
    global http_server
    http_server = HTTPServer(tornado_app)

    try:
        http_server.listen(options.port)
    except Exception, ex:
        logger.warn("listen port:%d failed with:%s", options.port, str(ex))
        return
    def _release_chanel_message(self):
        """
        释放超时消息

        :return:
        """
        loop_count2 = 1
        current_time = time.time()
        for k, v in self.chanel_message.items():
            i = 0
            loop_count1 += 1
            while i < len(v):
                #
                # 增加循环退出逻辑
                #
                loop_count1 += 1
                if loop_count1 > self.loop_count:
                    logger.warn("loop count exceed %d for _release_chanel_message_1", self.loop_count)
                    break

                (_, old_time) = v[i]
                diff_time = current_time - old_time
                if diff_time > self.pubsub_timeout:
                    logger.warn("remove timeout for id:%s", str(k))
                    del v[i]
                    #continue
                    break

                i += 1

            if len(v) <= 0:
                del self.chanel_message[k]

            #
            # 增加循环退出逻辑
            #
            loop_count2 += 1
            if loop_count2 > self.loop_count:
                logger.warn("loop count exceed %d for _release_chanel_message_2", self.loop_count)
                break