down voteaccepted

Just use redis.Redis. It uses a connection pool under the hood, so you don't have to worry about managing at that level.

If you absolutely have to use a low level connection, you need to do the response handling that is normally done for you by redis.Redis.

Here's an example of executing a single command using the low level connection:

def execute_low_level(command, *args, **kwargs):
connection = redis.Connection(**kwargs)
try:
connection.connect()
connection.send_command(command, *args) response = connection.read_response()
if command in redis.Redis.RESPONSE_CALLBACKS:
return redis.Redis.RESPONSE_CALLBACKS[command](response)
return response finally:
del connection

Example usage:

response = execute_low_level(
'HGET', 'redis:key', 'hash:key', host='localhost', port=6379)

But as I said before, redis.Redis is the way to go in 99.9% of cases.

you dont need worry about it when you use ConnectionPool.look at the source code:

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)

finally,every connection will release to the pool no matter what you do, and it will assign to other client.

最新文章

  1. SSIS 文件系统任务无法使用变量配置目标路径
  2. javascript学习4
  3. Java中Object类
  4. 【转】Velocity模板(VM)语言介绍
  5. C# Devexpress学习--LabelControl
  6. string.length()与-1比较为什么会出现匪夷所思的结果
  7. Computer Vision的尴尬---by林达华
  8. javascript对链接进行动态处理化
  9. cocos2d-x-2.2的SimpleAudioEngine::sharedEngine()->playEffect()计划中断bug
  10. myeclipse 2013 SR2 安装svn
  11. 写一个类时什么时候需要重写toString
  12. python-day2数据类型
  13. nginx重写rewrite的[emerg] unknown directive
  14. java常见题目总结
  15. rsa公钥和私钥的生成
  16. C/C++字符串使用整理
  17. Android实现两次点击返回键提示退出
  18. 自学Linux Shell1.2-Linux目录结构
  19. jenkins+git 构建项目
  20. angularjs中安卓原生APP调用H5页面js函数,js写法应注意

热门文章

  1. Thread 1 cannot allocate new log 的处理办法
  2. CodeForce 448C 木片填涂问题
  3. 洛谷P2058 海港
  4. linux mail 发邮件
  5. windows7 下安装使用Redis
  6. 前端学习之-- JavaScript
  7. python学习之-- redis模块基本介绍
  8. xml文件的schema也是经过jdk编译器编译的,如果xsd没引入完整,而xml中又用到了这些标签,就会编译不通过啊。
  9. 过滤器链chain.doFilter(request,response)含义
  10. 【转】Web Worker javascript多线程编程(一)