python操作Redis之连接池

python操作Redis之连接池

一. python操作Redis之连接池

redis-py使用connection pool来管理对一个redis server的所有连接,避免每次建立、释放连接的开销。默认,每个Redis实例都会维护一个自己的连接池。可以直接建立一个连接池,然后作为参数Redis,这样就可以实现多个Redis实例共享一个连接池

 
# 连接池
# 把他做成单例,写在一个文件里面,import它
import redis

# 拿到一个redis的连接池
Pool = redis.ConnectionPool(host='127.0.0.1', port=6379, max_connections=10)

# 从池子中拿一个链接
conn = redis.Redis(connection_pool=pool,decode_responses=True)
print(conn.get('name').decode('utf-8'))

# 如果想要并发操作,就需要写成单列,以模块导入就是一个单例,把他做成单例,写在一个文件里面,import它,就是一个单例
 

Python中设置Redis连接超时时间

当配置出现问题,或者redis服务挂了的时候,那么对redis的操作就会一直没有响应,那么可以通过设置redis的连接池的连接超时时间参数socket_connect_timeout

再通过ping()方法来检查Redis的连接有效性。

# 这里把连接时间设置为1秒
pool = redis.ConnectionPool(host='127.0.0.1', port=6379, db=0, socket_connect_timeout=1)
rs = redis.Redis(connection_pool=pool)
try:
    re.ping()
except TimeoutError:
    pring('redis connection timeout')


使用阻塞连接池

前面介绍了Redis客户端可以通过连接池方式初始化,在redis-py中提供了多种连接池以满足实际需求,这里介绍使用阻塞连接池(BlockingConnectionPool)操作Redis。

阻塞连接池的特点是:当连接池中没有空闲的连接时,会等待timeout秒,直到获取到连接或超时报错。

import redis
import threading


class RedaisExexThread(threading.Thread):

    def __init__(self, redis_client):
        threading.Thread.__init__(self)
        self.redis_client = redis_client

    def run(self):
        res = self.redis_client.get('foo')
        print(res)


def main():
    pool = redis.BlockingConnectionPool(host='localhost', port=6379, db=0, max_connections=2, timeout=5)
    redis_client = redis.Redis(connection_pool=pool)

    thread1 = RedaisExexThread(redis_client)
    thread2 = RedaisExexThread(redis_client)
    thread3 = RedaisExexThread(redis_client)
    thread4 = RedaisExexThread(redis_client)
    thread5 = RedaisExexThread(redis_client)
    thread6 = RedaisExexThread(redis_client)
    thread7 = RedaisExexThread(redis_client)
    thread8 = RedaisExexThread(redis_client)
    thread9 = RedaisExexThread(redis_client)
    thread10 = RedaisExexThread(redis_client)

    thread1.start()
    thread2.start()
    thread3.start()
    thread4.start()
    thread5.start()
    thread6.start()
    thread7.start()
    thread8.start()
    thread9.start()
    thread10.start()


if __name__ == '__main__':
    main()

这里创建了一个阻塞连接池,其容量为2,超时等待时间为5秒。用10个线程并发的方式测试阻塞连接池,可以正常使用。感兴趣的可以用普通连接池测试下,看会出现什么问题。

另外根据文档描述,这个阻塞连接池可以给多个客户端使用,实际测试也是可以的。感兴趣可以自己试下。

 

redis-py的使用很简单,只需要用redis.Redis建立连接,即可对Redis进行操作。例如:

import redis
r = redis.Redis(host='localhost', port=6379, db=0)
r.set('foo', 'bar')
r.get('foo')

默认情况下,Python3返回bytes,Python2返回str

多线程使用

redis-py是线程安全的Redis客户端,可以放心的在多线程代码中使用。

import redis
import threading


class RedaisExexThread(threading.Thread):

    def __init__(self, redis_client):
        threading.Thread.__init__(self)
        self.redis_client = redis_client

    def run(self):
        res = self.redis_client.get('foo')
        print(res)


def main():
    redis_client = redis.Redis(host='localhost', port=6379, db=0)

    thread1 = RedaisExexThread(redis_client)
    thread2 = RedaisExexThread(redis_client)
    thread3 = RedaisExexThread(redis_client)
    thread4 = RedaisExexThread(redis_client)

    thread1.start()
    thread2.start()
    thread3.start()
    thread4.start()


if __name__ == '__main__':
    main()

python redis-py模块使用详解

原文地址:https://www.cnblogs.com/leijiangtao/p/4668771.html