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 ...
随机推荐
- [HDU4348]To the moon(主席树)
传送门 对于这个题,显然要打lazy标记了,但是lazy标记pushdown的时候肯定会增加一大堆节点,然后就MLE了.(题解这么说的,我其实不会pushdown) 所以,就换另一种方式,把标记直接打 ...
- 【DFS+剪枝】Square
https://www.bnuoj.com/v3/contest_show.php?cid=9154#problem/J [题意] 给定n个木棍,问这些木棍能否围成一个正方形 [Accepted] # ...
- Python基础教程笔记——第3章:使用字符串
字符串是不可修改的,标准序列操作(索引,分片,判断成员资格,求长度,取最大值 最小值)对字符串都是有效的. 格式化字符串,类似于C语言的输出是的感觉. >>> format=&quo ...
- MongoDB基本管理命令操作
1. 查看所有数据库: show dbs 或: show databases 注意: 该命令不会显示新创建的空数据库,若想显示需要向空数据库插入一些数据. MongoDB中默认的数据库为test,若果 ...
- 【Java源码】集合类-优先队列PriorityQueue
一.类继承关系 public class PriorityQueue<E> extends AbstractQueue<E> implements java.io.Serial ...
- jenkins修改日志级别方法
1.jenkins日志有时候也会消耗掉很大内存,在传输时也会消耗掉大量带宽,如图,300+M的日志大小,太夸张了吧 2.修改日志级别的方法: 在配置文件里修改,重启后永久生效,配置路径:/etc/sy ...
- Python遍历路径下文件并转换成UTF-8编码
http://www.cnblogs.com/wuyuegb2312/archive/2013/01/11/2856772.html 开始学Python,这篇文章来自于应用需求. os.walk很方便 ...
- POJ 1284 Primitive Roots (求原根个数)
Primitive Roots 题目链接:id=1284">http://poj.org/problem?id=1284 利用定理:素数 P 的原根的个数为euler(p - 1) t ...
- 单点登录cas常见问题(二) - 子系统是否会频繁訪问cas中心?
这个问题的完整描写叙述是:用户成功登陆后.在訪问子系统的受限资源时,还须要訪问cas中心么,即子系统是否还会频繁訪问cas中心.cas中心会不会压力太大? 答案是:不会. 假设用户通过子系统A登录了c ...
- LightOj 1027 A Dangerous Maze【概率】
题目链接:http://www.lightoj.com/volume_showproblem.php? problem=1027 题意: 你面前有n个门,每一个相应一个数字,若为正xi.代表xi分钟后 ...