更多的细节python
链接池:web
在幕后,redis-py 使用链接池管理链接到redis-server的链接.默认, 一旦你建立了一个Redis的实例 ,这个实例相应有本身的链接池。你能够重写此行为,在建立一个Redis实例的时候指定一个建立的链接池,告诉这个实例是使用哪一个链接。(个人理解:若是存在多个redis-server,指定链接哪一个)redis
>>> pool = redis.ConnectionPool(host='localhost', port=6379, db=0) >>> r = redis.Redis(connection_pool=pool)
链接:数据库
ConnectionPoll管理一组链接,redis-py提供两种方式链接到redis-server.安全
一种是(也是默认的)TCP 套接字类型服务器
另外一种是使用 UnixDomainSocket链接。经过传递unix_socket_path参数,这是一个字符串,表明unix domain socket
文件。 另外确保在redis.conf定义unixsocket,默认是注释掉的。(这个俺不懂,接触的少)app
>>> r = redis.Redis(unix_socket_path='/tmp/redis.sock')
您能够建立本身的链接子类。若是你想控制套接字的行为在一个异步框架将会很是有用。框架
实例化一个客户端类使用你本身的链接,您须要建立一个链接池,经过你的connection_class类,还有相应的参数dom
>>> pool = redis.ConnectionPool(connection_class=YourConnectionClass, your_arg='...', ...)
解析器异步
Parser classes provide a way to control how responses from the Redis server are parsed. redis-py ships with two parser classes, the PythonParser and the HiredisParser. By default, redis-py will attempt to use the HiredisParser if you have the hiredis module installed and will fallback to the PythonParser otherwise.
解析器类提供了怎么样解析从Redis-server服务器返回的数据。
redis-py提供了两种解析器类,PythonParser 和 HiredisParser。默认使用HiredisParser,若是没有安装这个第三方模块就会使用PythonParser。(内部会尝试倒入HiredisParser,失败了就使用默认的)
Hiredis is a C library maintained by the core Redis team. Pieter Noordhuis was kind enough to create Python bindings. Using Hiredis can provide up to a 10x speed improvement in parsing responses from the Redis server. The performance increase is most noticeable when retrieving many pieces of data, such as from LRANGE or SMEMBERS operations.
Hiredis就是用C写,并且是redis核心组成员写的,速度是另外一个的10倍,(这么严重)当检索大量数据时性能提高最为明显 如这几个LRANGE or SMEMBERS
Hiredis is available on PyPI, and can be installed via pip or easy_install just like redis-py.
$ pip install hiredis
or
$ easy_install hiredis
响应回调
The client class uses a set of callbacks to cast Redis responses to the appropriate Python type. There are a number of these callbacks defined on the Redis client class in a dictionary called RESPONSE_CALLBACKS.
客户端类用一组回调函数处理Redis返回的数据,同时转换成合适的python的类型。在Redis客户端类中定义了不少这种回调函数。(实际是放在StrictRedis类中,放在RESPONSE_CALLBACKS中,这是个字典)
Custom callbacks can be added on a per-instance basis using the set_response_callback method. This method accepts two arguments: a command name and the callback. Callbacks added in this manner are only valid on the instance the callback is added to. If you want to define or override a callback globally, you should make a subclass of the Redis client and add your callback to its REDIS_CALLBACKS class dictionary.
可使用每一个实例的set_response_callback方法添加自定义回调函数。回调函数接受两个参数:redis的命令和回调函数名。
def set_response_callback(self, command, callback):
"Set a custom Response Callback"
self.response_callbacks[command] = callback
若是你想定义一个新的回调或复写存在的回调函数,须要写一个Redis client继承原来的Redis client,添加你的回调放到REDIS_CALLBACKS中。
Response callbacks take at least one parameter: the response from the Redis server. Keyword arguments may also be accepted in order to further control how to interpret the response. These keyword arguments are specified during the command's call to execute_command. The ZRANGE implementation demonstrates the use of response callback keyword arguments with its "withscores" argument.
线程安全
Redis client instances can safely be shared between threads. Internally, connection instances are only retrieved from the connection pool during command execution, and returned to the pool directly after. Command execution never modifies state on the client instance.
redis客户端实例在现成之间安全共享。在内部,执行命令时从链接池中取出一个链接,使用完后再放回链接池。在客户端实例,命令执行不会修改状态.
However, there is one caveat: the Redis SELECT command. The SELECT command allows you to switch the database currently in use by the connection. That database remains selected until another is selected or until the connection is closed. This creates an issue in that connections could be returned to the pool that are connected to a different database.
可是有一个警告:Redis的select命令。select命令容许你从0号数据库切换到1号数据库使用当前的链接。0号数据库会被当前链接仍然选择直到其余链接选择它,或者当前的链接关闭。这就产生了一个问题,This creates an issue in that connections could be returned to the pool that are connected to a different database.
As a result, redis-py does not implement the SELECT command on client instances. If you use multiple Redis databases within the same application, you should create a separate client instance (and possibly a separate connection pool) for each database.
因此,redis-py没有实现select的命令。要是想在一个web应用程序中使用多个redis的数据库,应该建立一个单独的redis客户端为每一个数据库(多是一个单独的链接池)
red0 = redis.StrictRedis(
host='localhost', port=6379,
db=0, password=None, #用的是0号数据库
charset='utf-8'
)
red1 = redis.StrictRedis(
host='localhost', port=6379,
db=1, password=None,#用的是1号数据库
charset='utf-8'
)
It is not safe to pass PubSub or Pipeline objects between threads.
在线程之间传递PubSub 和 Pipeline是不安全的。