메뉴 여닫기
개인 메뉴 토글
로그인하지 않음
만약 지금 편집한다면 당신의 IP 주소가 공개될 수 있습니다.

Python redis sample: 두 판 사이의 차이

데브카페
새 문서: 기타 python redis example 세모데 2017.03.26 17:24 댓글수0 공감수 0 # -*- coding:utf-8 -*- import sys, random, time from redis import Redis, exceptions, RedisError from redis.sentinel import (Sentinel, SentinelConnectionPool,ConnectionError, MasterNotFoundError, SlaveNotFoundError) # Redis 접속 기본 설정값 listSentinel = [('10.0.10.1', 26379), ('10.0.10.2', 26379), ('10.0.10.3', 26379), ('10.0.0.2', 26379)] strServiceName = '...
 
잔글 개발노트 양식 일괄 적용
 
1번째 줄: 1번째 줄:
{{개발노트
|요약=python redis example
|핵심=
|분야=Python
|작성일=2024-10-04
}}


기타
기타

2026년 11월 12일 (목) 12:44 기준 최신판

DEVCAFE NOTE
Python
python redis example
KEY POINTS · 핵심 정리
작성일2024년 10월 4일

기타 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."