Txredisapi 异常:RuntimeError:超出最大递归深度

0 投票
1 回答
552 浏览
提问于 2025-04-17 12:08

我想在Redis中删除所有的键,除了几个特定的键,但我遇到了一个异常:

  ... File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/twisted/protocols/basic.py", line 572, in dataReceived
    return self.rawDataReceived(data)
  File "build/bdist.macosx-10.6-intel/egg/txredisapi/protocol.py", line 184, in rawDataReceived

  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/twisted/protocols/basic.py", line 589, in setLineMode
    return self.dataReceived(extra)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/twisted/protocols/basic.py", line 564, in dataReceived
    why = self.lineReceived(line)
  File "build/bdist.macosx-10.6-intel/egg/txredisapi/protocol.py", line 134, in lineReceived

exceptions.RuntimeError: maximum recursion depth exceeded

这是我的代码:

@defer.inlineCallbacks
def resetAll(self):
    dict=yield self.factory.conn.keys()        
    for xyz in dict:
        if xyz<>"game" and xyz<>"people" and xyz<>"said":
            val = yield self.factory.conn.delete(xyz)

# ...

if __name__ == '__main__':
    from twisted.internet import reactor 
    conn = txredisapi.lazyRedisConnectionPool(reconnect = True)
    factory = STSFactory(conn)
    factory.clients = []

    print "Server started"
    reactor.listenTCP(11000,factory)
    reactor.listenTCP(11001,factory)
    reactor.listenTCP(11002,factory)
    reactor.run()

当我用大约725个键调用resetAll函数时,就出现了这个异常。如果键的数量少一些,比如200个,就不会出现这个问题。有人知道这是怎么回事吗?谢谢。

1 个回答

1

在一台有根权限的Linux或Mac电脑上,确保安装了Python和Git,然后在终端里试试这个:

cd
git clone https://github.com/andymccurdy/redis-py.git redis-py
cd redis-py
sudo python setup.py install

在后台,redis-py使用一个连接池来管理与Redis服务器的连接。默认情况下,每次你创建一个Redis实例,它都会创建自己的连接池。你可以改变这个默认行为,通过将一个已经创建好的连接池实例传递给Redis类的connection_pool参数,来使用现有的连接池。

示例(保存为delkeys.py):

#!/usr/bin/python
import redis

pool = redis.ConnectionPool(host='localhost', port=6379, db=0)
r = redis.Redis(connection_pool=pool)
keys = r.keys()
for key in keys:
    if key<>"game" and key<>"people" and key<>"said":
        r.del(key)

请注意,我还没有测试这个代码,但你的反馈可能会影响最终的解决方案,或者你可以在此基础上继续。使用redis-cli监控,以确保一切正常。

撰写回答