0

我正在尝试删除 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()

当我在 Redis 中使用大约 725 个键调用 resetAll 函数时,我触发了异常。对于较低的数字,如 200 等,它不会被解雇。有人知道发生了什么吗?谢谢。

4

1 回答 1

1

在具有 root 访问权限并安装了 Python 和 Git 的 Linux/Mac 计算机的终端上尝试此操作:

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 中进行监控。

于 2012-02-02T20:02:10.187 回答