Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Redis interaction

Tags:

python

redis

I want to write application in python which uses redis. I googled but I could not find any results for my question. Usually, I do this:

import redis

rs = redis.Redis('localhost')

then do all gets and sets. But can I in redis do something like this:

rs1 = redis.Redis('app1')
rs2 = redis.Redis('app2')

I mean, I want to use two or more instances, each of which stores different things (for example rs1 for urls, rs2 for headers, etc...). And also I want to know how to delete all keys (for example in rs1 delete all records). Any good tutorial, resource? Note: I need to use redis because I need to preform fast check and store, like url-seen for crawler.

like image 953
torayeff Avatar asked May 30 '12 11:05

torayeff


People also ask

Can Redis handle multiple connections?

Redis can handle many connections, and by default, Redis has a maximum number of client connections set at 10,000 connections. You can set the maximum number of client connections you want the Redis server to accept by altering the maxclient from within the redis. conf file.

Can Redis store Python objects?

Actually, you can store python objects in redis using the built-in module pickle. Here is example. This is dangerous: unpickling can execute code. The JSON solution is more robust.


1 Answers

As showed in the getting started section of the docs redis.Redis and redis.StrictRedis both take an integer db argument as a constructor parameter. That will get you an effectively silo'ed instance.

You could do something like the following:

rs1 = redis.Redis(host="localhost", db=0)
rs2 = redis.Redis(host="localhost", db=1)

flushdb() will clear all the keys for the database you are connected to, while flushall() will clear all the keys for every database.

like image 86
kojiro Avatar answered Oct 13 '22 16:10

kojiro