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.

Redis in python, how do you close the connection?的更多相关文章

  1. Redis的Python实践,以及四中常用应用场景详解——学习董伟明老师的《Python Web开发实践》

    首先,简单介绍:Redis是一个基于内存的键值对存储系统,常用作数据库.缓存和消息代理. 支持:字符串,字典,列表,集合,有序集合,位图(bitmaps),地理位置,HyperLogLog等多种数据结 ...

  2. Redis的Python客户端redis-py的初步使用

    1. Redis的安装 sudo pip install redis sudo pip install hiredis Parser可以控制如何解析redis响应的内容.redis-py包含两个Par ...

  3. redis与python交互

    import redis #连接 r=redis.StrictRedis(host="localhost",port=6379,password="sunck" ...

  4. LinuxMint上安装redis和python遇到的一些问题

    今天在安装Redis和Python上遇到了些问题,解决后记录下来. 环境:LinuxMint 18.3 安装redis sudo wget http://download.redis.io/relea ...

  5. Redis在python中的使用

    一 简介 redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorted ...

  6. redis & macOS & python

    redis & macOS & python how to install python 3 on mac os x? https://docs.python.org/3/using/ ...

  7. Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused (Connection refused)

    一.linux中配置redis,使用java连接测试时报错: Exception in thread "main" redis.clients.jedis.exceptions.J ...

  8. 解决python pymysql.err.OperationalError: (2013, 'Lost connection to MySQL server during query')

    解决python pymysql.err.OperationalError: (2013, 'Lost connection to MySQL server during query') 学习了:ht ...

  9. Redis的Python客户端redis-py

    1. 安装 1. redis-py a. 使用easy_install 1 sudo easy_install redis  b. 源码安装 1 2 3 git clone https://githu ...

随机推荐

  1. HDU 3264 区间内的最大最小之差

    题目链接:http://poj.org/problem?id=3264 题目大意:在给定一堆牛的数量以及其高度的时候,每次给定一段区间,求这个区间内最高的牛和最矮的牛的高度之差为多少. 可以直接利用R ...

  2. 线段上的格点 辗转相除法(GCD)

    /*问题描述:线段上的格点给定平面上的两个格点 P1 = (x1, y1) ; P2 = (x2, y2) 线段P1 P2上,除P1 和 P2以外一共有几个格点*//*分析过程在格点上画P1(0,5) ...

  3. python监控tomcat日记文件

    最近写了一个用python监控tomcat日记文件的功能 实现的功能: 监控日记文件中实时过来的记录,统计每分钟各个接口调用次数,统计结果插入oracle #!/usr/bin/python # -* ...

  4. ListView更新问题

    ListView和Adapter对象均具备有对象更新方法 ListView对象列表的更新方法1.invalidate();--重绘组件2.invlidateView()--重绘组件并包含所有的View ...

  5. struts2常用类型的Result

    2.2.1. dispatcher dispatcher类型是用于转发的Result,可以将请求转发给JSP.这种类型的Result对应的类为 ServletDispatcherResult,它是St ...

  6. [Bzoj4182]Shopping(点分治)(树上背包)(单调队列优化多重背包)

    4182: Shopping Time Limit: 30 Sec  Memory Limit: 128 MBSubmit: 374  Solved: 130[Submit][Status][Disc ...

  7. INFO org.apache.hadoop.ipc.RPC: Server at master/192.168.200.128:9000 not available yet, Zzzzz...

    hadoop 启动时namenode和datanode可以启动,使用jps命令也可以看到进程,但是在浏览器中输入master:50070却没有显示datanode 查看datanode的log日志: ...

  8. Meteor教程

    Meteor 是一个构建在 Node.js 之上的平台,用来开发实时网页程序.Meteor 程序位于数据库和用户界面之间,保持二者之间的数据同步更新. 因为 Meteor 是基于 Node.js 开发 ...

  9. cef3的各个接口你知道几个

    CEF3基本的框架包含C/C++程 序接口,通过本地库的接口来实现,而这个库则会隔离宿主程序和 Chromium&Webkit的操作细节.它在浏览器控件和宿主程序之间提供紧密的整合,它支持用户 ...

  10. grails的criteria和hql查询

    grails在查询方面也保留了hibernate的hql和criteria查询功能.hql自然不必多说基本不会有什么变化,grails的criteria查询在hibernate上面做了微调使用起来更加 ...