python连接redis
import redisimport 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]