행위

Python redis sample

DB CAFE

기타 python redis example 세모데 2017.03.26 17:24 댓글수0 공감수 0

  1. -*- coding:utf-8 -*-

import sys, random, time from redis import Redis, exceptions, RedisError from redis.sentinel import (Sentinel, SentinelConnectionPool,ConnectionError,

                           MasterNotFoundError, SlaveNotFoundError)
  
  1. Redis 접속 기본 설정값

listSentinel = [('10.0.10.1', 26379), ('10.0.10.2', 26379), ('10.0.10.3', 26379), ('10.0.0.2', 26379)] strServiceName = 'stn-master' strRedisPass = '1234' nDB = 0

nMaxUser = 1000

sentinel = Sentinel(listSentinel, socket_timeout=0.1) try:

   #sentinel.discover_master(strServiceName) # No need for this
   #sentinel.discover_slaves(strServiceName)
   master = sentinel.master_for(strServiceName, password=strRedisPass, db=nDB, socket_timeout=0.1)
   slave = sentinel.slave_for(strServiceName, password=strRedisPass, db=nDB, socket_timeout=0.1)
  

except MasterNotFoundError:

   print 'Master not found or Sentinel instances not runnung'
   sys.exit()

except SlaveNotFoundError:

   print 'Slave not found or Sentinel instances not runnung'
   sys.exit()

except ConnectionError:

   print 'Connection Error. Check if Sentinel instances are running'
   sys.exit()
  

start_time = time.time()

for n in range(1, nMaxUser):

   for m in range(1, random.randint(0, 5)):
       master.hset( "cart.user:"+str(n), random.randint(1, 300), random.randint(1, 5) )
  

time_elapsed_1 = time.time() - start_time

start_time = time.time()

for n in range(1, nMaxUser):

   slave.hgetall("cart.user:"+str(n))
  

time_elapsed_2 = time.time() - start_time

count = 0

for n in range(1, nMaxUser):

   data = slave.hgetall("cart.user:"+str(n))
   if len(data) > 0:
       count = count + 1
       print count, " [Cart.user:"+str(n)+"] ", data
  

print "---------------------------------------------------" print "[Time for writing]: ", time_elapsed_1, " sec., [Time for reading]: ", time_elapsed_2, " sec."