示例#1
0
    def _replacement(self):
        """进行换入换出
        在缓存储量超过警戒线,未达上限时执行
        """
        now = int(time.time())
        to_eliminate = []
        for id in self.db:
            if now - self.db[id].active_time >= self._BUFFER_EXPIRE_TIME:
                #淘汰不活跃的数据
                to_eliminate.append(id)

            #一次淘汰,最多淘汰一个固定个数,避免耗时过多
            if len(to_eliminate) >= self._BUFFER_ELIMINATE_NUM:
                break

        for id in to_eliminate:
            del self.db[id]

        count = len(to_eliminate)
        if count == 0:
            logger.warning("Database replacement failed, no inactive item[active num=%d]" %
                    len(self.db))
        else:
            logger.trace("Database replacement succeed[active num=%d][eliminate num=%d]" %
                    (len(self.db), count))
示例#2
0
 def connect(self, ip, port, db='0', password=None, timeout=1):
     """初始化
     Args:
         timeout : 连接超时事件,单位 s
     """
     self.redis = RedisAgent()
     self.redis.connect(ip, port, db, password, timeout)
     logger.trace("Connect to redis success[addr=%s:%s]" % (ip, port))
示例#3
0
def netserviceHandle(target):
    """注册方法
    @param target: function
    """
    try:
        _netservice.mapTarget(target)
    except Exception as e:
        logger.fatal("Register method to net service failed[method=%s]" %
                     target.__name__)
        sys.exit(-1)

    logger.trace("Register method to net service[method=%s]" % target.__name__)
    return target
示例#4
0
    def load(self, path, file_conf):
        """读入一个文件,生成对应的 dict
        @param path: 文件路径
        @param file_conf: 文件配置
        """
        file_name = "%s%s" % (path, file_conf.get("name"))
        dicts_conf = file_conf.get("dicts")

        data = xlrd.open_workbook(file_name)
        start = time.time()

        for conf in dicts_conf:
            self._gen_dict(data, conf)

        duration = int((time.time() - start) * 1000)
        logger.trace("Finish load file[file=%s][consume=%d ms]" %
                (file_name, duration))
示例#5
0
def load_data(conf_path):
    if isinstance(data_loader, DataLoader):
        config = json.load(open(conf_path, 'r'))
        path = config.get("path")[GlobalObject().version]
        files_name = os.listdir(path)
        files_conf = config.get("files")

        for conf in files_conf:
            name = conf.get("name")
            if name  not in files_name:
                logger.warning("Data file not exist[name=%s]" % name)
                continue

            # try:
            data_loader.load(path, conf)
            # except Exception as e:
            #     logger.fatal("Load data file failed[file=%s][exception=%s]" % (name, e))
            #     sys.exit(-1)
    else:
        start = time.time()
        data_loader.load(conf_path, GlobalObject().version)
        duration = int((time.time() - start) * 1000)
        logger.trace("cdata_loader load succeed[consume=%d]" % duration)
示例#6
0
from portal.command_table import init_command
from portal.protocol import DataPacker
from portal.process import user_login
from portal.process import user_logout
from portal.process import data_received

#初始化 command id 表
command_file = GlobalObject().json_config.get("command_data")
init_command(command_file)

#定制 Factory 的方法
GlobalObject().netfactory.doDataReceived = data_received
GlobalObject().netfactory.doConnectionMade = user_login
GlobalObject().netfactory.doConnectionLost = user_logout

#初始化 token redis
_token_redis_conf = GlobalObject().cache_config.get("token_redis")
TokenRedisAgent().connect(_token_redis_conf.get('ip'),
                          _token_redis_conf.get('port'),
                          _token_redis_conf.get('db'),
                          _token_redis_conf.get('password'))

#配置数据解析协议
dataprotocl = DataPacker()
GlobalObject().netfactory.setDataProtocl(dataprotocl)

import portal.service
import portal.forward

logger.trace("Load portal module succeed")
示例#7
0
Created on 2016-05-17
@Author: jiangtaoran([email protected])
@Brief : Unit 模块逻辑入口
"""

from twisted.internet import reactor
from firefly.server.globalobject import GlobalObject
from utils import logger
from datalib.data_proxy4redis import DataRedisAgent
from datalib.data_loader import load_data
from datalib.table import TableDescription
from datalib.global_data import DataBase
from unit.city_view import CityData
import unit.service

_redis_conf = GlobalObject().cache_config.get('data_redis')
DataRedisAgent().connect(_redis_conf.get('ip'), _redis_conf.get('port'),
                         _redis_conf.get('db'), _redis_conf.get('password'))

_data_conf = GlobalObject().json_config.get('dataconf')
load_data(_data_conf)

_table_conf = GlobalObject().json_config.get("tableconf")
table = TableDescription()
table.parse(_table_conf)
GlobalObject().table_desc = table

DataBase().register_datatype(CityData)

logger.trace("Load Unit module succeed")
示例#8
0
load_data(_data_conf)

#初始化 DB
_table_conf = GlobalObject().json_config.get("tableconf")
table = TableDescription()
table.parse(_table_conf)
GlobalObject().table_desc = table

DataBase().register_datatype(UserData)
DataBase().register_basic_datatype(BasicData)

_pay_conf = GlobalObject().json_config.get("payconf")
PayLogic().init_pay(_pay_conf)

_chat_conf = GlobalObject().json_config.get("chatconf")
ChatPool().load_conf(_chat_conf)

event.register_lucky_event()

logger.trace("Load App module succeed")

# def test_thread(x):
#     print "x"
#     print x
#     while (1):
#         print "run in thread"
#         import time
#         time.sleep(1)

# reactor.callFromThread(test_thread, 3)
示例#9
0
文件: load.py 项目: hw233/test-2
@Brief : common模块逻辑入口
"""

from twisted.internet import reactor
from firefly.server.globalobject import GlobalObject
from utils import logger
from datalib.data_proxy4redis import DataRedisAgent
from datalib.table import TableDescription
from datalib.data_loader import load_data
from datalib.global_data import DataBase
from common.common_view import CommonData
import common.service

_redis_conf = GlobalObject().cache_config.get('data_redis')
DataRedisAgent().connect(_redis_conf.get('ip'), _redis_conf.get('port'),
                         _redis_conf.get('db'), _redis_conf.get('password'))

#读入Excel
_data_conf = GlobalObject().json_config.get('dataconf')
load_data(_data_conf)

#初始化 DB
_table_conf = GlobalObject().json_config.get("tableconf")
table = TableDescription()
table.parse(_table_conf)
GlobalObject().table_desc = table

DataBase().register_datatype(CommonData)

logger.trace("Load Common module succeed")