亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

python redis之連接池的原理

系統 1616 0

python redis之連接池的原理

?

轉載地址

什么是連接池

通常情況下, 當我們需要做redis操作時, 會創建一個連接, 并基于這個連接進行redis操作, 操作完成后, 釋放連接,

一般情況下, 這是沒問題的, 但當并發量比較高的時候, 頻繁的連接創建和釋放對性能會有較高的影響

于是, 連接池就發揮作用了

連接池的原理是, 通過預先創建多個連接, 當進行redis操作時, 直接獲取已經創建的連接進行操作, 而且操作完成后, 不會釋放, 用于后續的其他redis操作

這樣就達到了避免頻繁的redis連接創建和釋放的目的, 從而提高性能了

?

原理

那么, 在redis-py中, 他是怎么進行連接池管理的呢

連接池使用

首先看下如何進行連接池操作的

              rdp = redis.ConnectionPool(host=
              
                '
              
              
                127.0.0.1
              
              
                '
              
              , port=6379, password=
              
                '
              
              
                xxxxx
              
              
                '
              
              
                )
rdc 
              
              = redis.StrictRedis(connection_pool=
              
                rdp)
rdc.set(
              
              
                '
              
              
                name
              
              
                '
              
              , 
              
                '
              
              
                Yi_Zhi_Yu
              
              
                '
              
              
                )
rdc.get(
              
              
                '
              
              
                name
              
              
                '
              
              )
            

原理解析

當redis.ConnectionPool 實例化的時候, 做了什么

              
                def
              
              
                __init__
              
              (self, connection_class=Connection, max_connections=
              
                None,
                 
              
              **
              
                connection_kwargs):
        max_connections 
              
              = max_connections 
              
                or
              
               2 ** 31
        
              
                if
              
              
                not
              
               isinstance(max_connections, (int, long)) 
              
                or
              
               max_connections <
              
                 0:
            
              
              
                raise
              
               ValueError(
              
                '
              
              
                "max_connections" must be a positive integer
              
              
                '
              
              
                )

        self.connection_class 
              
              =
              
                 connection_class
        self.connection_kwargs 
              
              =
              
                 connection_kwargs
        self.max_connections 
              
              = max_connections
            

這個連接池的實例化其實未做任何真實的redis連接, 僅僅是設置最大連接數, 連接參數和連接類

StrictRedis 實例化的時候, 又做了什么

              
                def
              
              
                __init__
              
              (self, ...connection_pool=
              
                None...):
        
              
              
                if
              
              
                not
              
              
                 connection_pool:
            ...
            connection_pool 
              
              = ConnectionPool(**
              
                kwargs)
        self.connection_pool 
              
              = connection_pool
            

以上僅保留了關鍵部分代碼

可以看出, 使用StrictRedis 即使不創建連接池, 他也會自己創建

到這里, 我們還沒有看到什么redis連接真實發生

繼續

下一步就是 set ?操作了, 很明顯, 這個時候一定會發生redis連接(要不然怎么set)

              
                def
              
               set(self, name, value, ex=None, px=None, nx=False, xx=
              
                False):
    ...
    
              
              
                return
              
               self.execute_command(
              
                '
              
              
                SET
              
              
                '
              
              , *pieces)
            

我們繼續看看 execute_command

              
                def
              
               execute_command(self, *args, **
              
                options):
        
              
              
                "
              
              
                Execute a command and return a parsed response
              
              
                "
              
              
                
        pool 
              
              =
              
                 self.connection_pool
        command_name 
              
              =
              
                 args[0]
        connection 
              
              = pool.get_connection(command_name, **
              
                options)
        
              
              
                try
              
              
                :
            connection.send_command(
              
              *
              
                args)
            
              
              
                return
              
               self.parse_response(connection, command_name, **
              
                options)
        
              
              
                except
              
              
                 (ConnectionError, TimeoutError) as e:
            connection.disconnect()
            
              
              
                if
              
              
                not
              
               connection.retry_on_timeout 
              
                and
              
              
                 isinstance(e, TimeoutError):
                
              
              
                raise
              
              
                
            connection.send_command(
              
              *
              
                args)
            
              
              
                return
              
               self.parse_response(connection, command_name, **
              
                options)
        
              
              
                finally
              
              
                :
            pool.release(connection)
              
            

終于, 在這我們看到到了連接創建

              connection = pool.get_connection(command_name, **options)
            

這里調用的是 ConnectionPool 的get_connection

              
                def
              
               get_connection(self, command_name, *keys, **
              
                options):
        
              
              
                "
              
              
                Get a connection from the pool
              
              
                "
              
              
                
        self._checkpid()
        
              
              
                try
              
              
                :
            connection 
              
              =
              
                 self._available_connections.pop()
        
              
              
                except
              
              
                 IndexError:
            connection 
              
              =
              
                 self.make_connection()
        self._in_use_connections.add(connection)
        
              
              
                return
              
               connection
            

如果有可用的連接, 獲取可用的鏈接, 如果沒有, 創建一個

              
                def
              
              
                 make_connection(self):
        
              
              
                "
              
              
                Create a new connection
              
              
                "
              
              
                if
              
               self._created_connections >=
              
                 self.max_connections:
            
              
              
                raise
              
               ConnectionError(
              
                "
              
              
                Too many connections
              
              
                "
              
              
                )
        self._created_connections 
              
              += 1
        
              
                return
              
               self.connection_class(**self.connection_kwargs)
            

終于, 我們看到了, 在這里創建了連接

在ConnectionPool的實例中, 有兩個list, 依次是 _available_connections ,? _in_use_connections ,

分別表示 可用的連接集合 正在使用的連接集合 , 在上面的 get_connection 中, 我們可以看到獲取連接的過程是

  1. 從可用連接集合嘗試獲取連接,
  2. 如果獲取不到, 重新創建連接
  3. 將獲取到的連接添加到正在使用的連接集合

上面是往 _in_use_connections 里添加連接的, 這種連接表示正在使用中, 那是什么時候將正在使用的連接放回到可用連接列表中的呢

這個還是在 execute_command 里, 我們可以看到在執行redis操作時, 在 finally 部分, 會執行一下

              pool.release(connection)
            

連接池對象調用 release 方法, 將連接從 _in_use_connections ?放回? _available_connections , 這樣后續的連接獲取就能再次使用這個連接了

release ?方法如下

              
                def
              
              
                 release(self, connection):
        
              
              
                "
              
              
                Releases the connection back to the pool
              
              
                "
              
              
                
        self._checkpid()
        
              
              
                if
              
               connection.pid !=
              
                 self.pid:
            
              
              
                return
              
              
                
        self._in_use_connections.remove(connection)
        self._available_connections.append(connection)
              
            

總結

至此, 我們把連接池的管理流程走了一遍, ConnectionPool通過管理 可用連接列表 ( _available_connections ) 和? 正在使用的連接列表 從而實現連接池管理

?


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 国产成人久久久精品一区二区三区 | 亚洲色啦啦狠狠网站 | 伊人久久一本大道 | 青娱乐伊人| 91久久青草精品38国产 | 欧美大片天天免费看视频 | 免费精品国产自产拍观看 | 九九色在线视频 | 亚洲综合在线一区 | 福利姬视频在线观看 | 国产精彩视频在线 | 综合久久久久综合体桃花网 | 精品视频一区二区三区在线播放 | 四虎永久免费观看紧急入口 | 国产精品视频第一区二区三区 | 婷婷久久精品 | 中文字幕不卡在线观看 | 日韩区 | 狠狠狠狼鲁欧美综合网免费 | 日韩中文字幕在线免费观看 | 亚洲一区小说区中文字幕 | 妖精视频在线观看网站 | 四虎影视在线影院在线观看 | 四虎国产精品永久在线看 | 亚洲精品人成在线观看 | 国产午夜精品久久久久九九 | 国产视频一区二区三区四区 | 精品国产一区二区三区香蕉沈先生 | 狠狠狠狠狠狠狠狠 | 国产精品久久久视频 | 护士日本xxxxx丰满hd4k | 最新91视频 | 六月婷婷色| 亚洲精品一区二区久久久久 | 九九热精彩视频 | 香蕉午夜| 国产成人小视频在线观看 | 五月婷婷激情综合网 | 日韩中文字幕视频在线 | 久久久久久综合成人精品 | 国产在线短视频 |