python連線redis,redis叢集
阿新 • • 發佈:2018-11-10
python連線redis:
import redis r = redis.Redis(host='192.168.50.181',port=6002) r.set('user_phone_14900000001','888888') r.get('user_phone_14900000001')
上面程式碼如果只執行一條資料是正常的,如果要用for迴圈批量更改資料連線redis叢集,會報錯redis.exceptions.ResponseError()
解決方案如下:
python連線redis叢集:
from rediscluster import StrictRedisCluster list= [ '14900000001', '14900000002', '14900000003', '14900000004', '14900000005', '14900000006', '14900000007', '14900000008', '14900000009', '14900000010' ] def redis_cluster(list): redis_nodes = [ {'host':'192.168.50.39','port':6002}, {'host':'192.168.50.181','port': 6002}, ] try: redisconn = StrictRedisCluster(startup_nodes=redis_nodes)except: print('error') for i in range(0,len(list)): redisconn.set('user_phone_'+list[i],'888888') print(redisconn.get('user_phone_'+list[i])) redis_cluster(list)
列印結果如下:
b'888888'
b'888888'
b'888888'
b'888888'
b'888888'
b'888888'
b'888888'
b'888888'
b'888888'
b'888888'
def redis_cluster(): redis_nodes = [ {'host':'192.168.50.39','port':6002}, {'host':'192.168.50.181','port': 6002}, ] try: redisconn = StrictRedisCluster(startup_nodes=redis_nodes) except: print('error') for i in range(14900000011,14900000021): redisconn.set('user_phone_'+str(i),'888888') print(redisconn.get('user_phone_'+str(i))) redis_cluster()