更多的細節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是不安全的。