I am trying to delete all keys except some in Redis, and I do get the following exception:
... 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
Here is the code:
@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()
When I call resetAll function with around 725 keys in Redis, I got the exception fired. With lower numbers like 200 etc it is not fired. Anybody has an idea what is happening? Thanks.
Try this on a terminal of a Linux/Mac computer with root access, and Python and Git installed:
cd
git clone https://github.com/andymccurdy/redis-py.git redis-py
cd redis-py
sudo python setup.py install
Behind the scenes, redis-py uses a connection pool to manage connections to a Redis server. By default, each Redis instance you create will in turn create its own connection pool. You can override this behavior and use an existing connection pool by passing an already created connection pool instance to the connection_pool argument of the Redis class.
#!/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)
Take this with the reminder that I haven't tested, but your comments may shape the final solution or you can go from here. Always monitor in redis-cli to be sure.