wyp
wyp

Reputation: 879

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.

Upvotes: 13

Views: 13410

Answers (1)

freakish
freakish

Reputation: 56477

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)

Upvotes: 29

Related Questions