Python ORM Storm 源码修改

安装 storm : pip install storm

目标:修改 Storm 源代码以支持自动重连
文件:python安装目录/site-packages/storm/database.py

在411行添加函数(可以不在这一行,只要是给class Connection添加函数就行):

def _check_and_reconnect(self):
    if not isinstance(self._database,storm.databases.mysql.MySQL):
        return 0
    try:
        self._raw_connection.ping()
    except Exception,e:
        try:
            self._raw_connection = self._database.raw_connect()
        except Exception,e:
            return 0
    self._state = STATE_CONNECTED
    return 1

 

在函数 _ensure_connected(self)中调用上面的函数:

def _ensure_connected(self):
    """Ensure that we are connected to the database.

    If the connection is marked as dead, or if we can't reconnect,
    then raise DisconnectionError.
    """
    if self._blocked:
        raise ConnectionBlockedError("Access to connection is blocked")
    self._check_and_reconnect()  #添加这一行即可,该函数只有这一处改动
    if self._state == STATE_CONNECTED:
        return
    elif self._state == STATE_DISCONNECTED:
        raise DisconnectionError("Already disconnected")
    elif self._state == STATE_RECONNECT:
        try:
            self._raw_connection = self._database.raw_connect()
        except DatabaseError, exc:
            self._state = STATE_DISCONNECTED
            self._raw_connection = None
            raise DisconnectionError(str(exc))
        else:
            self._state = STATE_CONNECTED

 

posted @ 2015-11-27 12:21  高山流的不是水  阅读(648)  评论(0编辑  收藏  举报