Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write to several database in Redis for python

I want to write some values into different database, this is my code:

import redis

r1 = redis.Redis(host='127.0.0.1', port=6379, db = 'db1')
r2 = redis.Redis(host='127.0.0.1', port=6379, db = 'db2')

numList = ['4', '3', '2', '1']

for num in numList:
   r1.lpush('a', num)
   r2.lpush('a', 'test')

print r1.lrange('a',start=0,end=-1)
print r2.lrange('a',start=0,end=-1)

Then I got this result:

['test', '1', 'test', '2', 'test', '3', 'test', '4']
['test', '1', 'test', '2', 'test', '3', 'test', '4']

Although I use different database, but for the same key, all the value is put in. Thank you.

like image 258
wyp Avatar asked Nov 01 '12 14:11

wyp


People also ask

Can Redis have multiple databases?

Redis comes with support for multiple databases, which is very similar to the concept in SQL databases. In SQL databases, such as MySQL, PostgreSQL, and Oracle, you can define a name for your databases. However, Redis databases are represented by numbers.

How does Redis handle multiple connections?

Large number of connectionsRedis is a single-threaded process based on an event loop where incoming client requests are handled sequentially. That means the response time of a given client becomes longer as the number of connected clients increases.


1 Answers

DBs are supposed to be zero-based numeric index (apperently the limit is 15). Try using

r1 = redis.Redis(host='127.0.0.1', port=6379, db = 0)
r2 = redis.Redis(host='127.0.0.1', port=6379, db = 1)
like image 58
freakish Avatar answered Oct 14 '22 04:10

freakish