Redis in python, how do you close the connection?
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?的更多相关文章
- Redis的Python实践,以及四中常用应用场景详解——学习董伟明老师的《Python Web开发实践》
首先,简单介绍:Redis是一个基于内存的键值对存储系统,常用作数据库.缓存和消息代理. 支持:字符串,字典,列表,集合,有序集合,位图(bitmaps),地理位置,HyperLogLog等多种数据结 ...
- Redis的Python客户端redis-py的初步使用
1. Redis的安装 sudo pip install redis sudo pip install hiredis Parser可以控制如何解析redis响应的内容.redis-py包含两个Par ...
- redis与python交互
import redis #连接 r=redis.StrictRedis(host="localhost",port=6379,password="sunck" ...
- LinuxMint上安装redis和python遇到的一些问题
今天在安装Redis和Python上遇到了些问题,解决后记录下来. 环境:LinuxMint 18.3 安装redis sudo wget http://download.redis.io/relea ...
- Redis在python中的使用
一 简介 redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorted ...
- redis & macOS & python
redis & macOS & python how to install python 3 on mac os x? https://docs.python.org/3/using/ ...
- 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 ...
- 解决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 ...
- Redis的Python客户端redis-py
1. 安装 1. redis-py a. 使用easy_install 1 sudo easy_install redis b. 源码安装 1 2 3 git clone https://githu ...
随机推荐
- [转]android 如何获取第三方app的sha1值
对于android 应用的sha1值和md5值的获取,如果是我们自己的应用,不论是获取测试的和正式的都是比较方便的.但是如何去获取别人开发的app的sha1和md5呢,并且我们只有apk有没有相关的文 ...
- php中memcache与memcached的区别 【收藏】
说法一: 两个不同版本的php的memcached的客户端 new memcache是pecl扩展库版本new memcached是libmemcached版本功能差不多 说法二: Mem ...
- iOS 混合变换旋转 CGAffineTransform
在ios 中, Core Graphics 提供了一系列的函数可以在一个变换的基础上做深层次的变换,如果做一个既要缩放又要旋转的变换,以下的方法比较实用. CGAffineTransformScale ...
- Django学习之 - 基础ORM
ORM操作参考: http://www.cnblogs.com/wupeiqi/articles/5246483.html 1:根据类自动创建数据库表,(类创建文件:models.py) 2:根据类对 ...
- BZOJ——2697: 特技飞行
http://www.lydsy.com/JudgeOnline/problem.php?id=2697 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: ...
- Permutations(排列问题,DFS回溯)
Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...
- codechef FUN WITH TREES
题目大意: 给一棵树root=1的树: 给一些操作:u v 的路径所有节点的node + val: 最后m个询问:u 节点(包括u) sum%mod 是多少. LCA + RMQ: 我们每次mark ...
- 寒武纪camp Day5
补题进度:6/10 A(状压dp) 题意: 有n个数字1,2,...,n,有m个限制(a,b),表示至少要有一个数字a排在数字b的前面 你需要构造出一个含有数字1~n的序列,数字可以重复多次,要求该序 ...
- 【.Net 学习系列】-- Windows身份模拟(WindowsIdentity.Impersonate)时读取Access数据库
参考资料: WindowsIdentity.Impersonate https://msdn.microsoft.com/zh-cn/library/w070t6ka(v=vs.110).aspx A ...
- ubuntu下打开eclipse·发现没有顶尖菜单项
在安装eclipse时,打开集成开发环境后没有菜单项. 网上些人说要写个shell脚步,感觉有点麻烦,其实就是少了一个环境变量 BUNTU_MENUPROXY. 在/etc/profile 里面新建这 ...