刘伟 发表于 2018-8-5 14:26:50

python连接redis

import redis  
import datetime
  
def withpipe(r):
  
    pipe = r.pipeline(transaction=True)
  
    for i in xrange(1, 1000):
  
      key = "key1_" + str(i)
  
      value = "value1_" + str(i)
  
      pipe.set(key, value)
  
    pipe.execute()
  

  
def withoutpipe(r):
  
    for i in xrange(1, 1000):
  
      key = "key2_" + str(i)
  
      value = "value2_" + str(i)
  
      r.set(key, value)
  

  
redis_config = {
  
    "host": "192.168.2.230",
  
    "port": 6379,
  
    "db": 0
  
}
  

  
if __name__ == "__main__":
  
    pool = redis.ConnectionPool(**redis_config)
  
    r1 = redis.Redis(connection_pool=pool)
  
    r2 = redis.Redis(connection_pool=pool)
  
    start = datetime.datetime.now()
  

  
    withpipe(r1)
  
    end = datetime.datetime.now()
  
    t_time = (end - start).microseconds
  
    print("withpipe time is: {0}".format(t_time))
  
    start = datetime.datetime.now()
  

  
    withoutpipe(r2)
  
    end = datetime.datetime.now()
  
    t_time = (end - start).microseconds
  
    print("withoutpipe time is: {0}".format(t_time))
页: [1]
查看完整版本: python连接redis