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 ...
随机推荐
- CodeForces 444C 节点更新求变化值的和
http://vjudge.net/problem/viewProblem.action?id=51622 题目大意: 给定一列n个数字,最初赋予值1到n 两个操作:1.将区间[l,r]内的数改为x, ...
- tree(poj 1741)
题意:给一颗n个节点的树,每条边上有一个距离v(v<=1000).定义d(u,v)为u到v的最小距离.给定k值,求有多少点对(u,v)使u到v的距离小于等于k. /* 照着点分治模板敲了敲,有很 ...
- BZOJ1703: [Usaco2007 Mar]Ranking the Cows 奶牛排名
n<=1000头牛各有一个未知值Ai,已知m<=10000条形如Ax>Ay的不等关系,求将整个序列排序的最少比较次数. Aa>Ab,Ab>Ac -------> A ...
- BZOJ3926 (后缀自动机)
BZOJ3926 诸神眷顾的幻想乡 Problem : 给一个n个节点的树(n<=10^5), 每个点有一种颜色(c<=10), 询问所有点对之间路径组成字符串的种类.保证叶子节点小于等于 ...
- mysql 常用管理命令
常见的管理mysql命令 (1)用于选择在MySQL工作区指定的数据库(选择数据库): USE Databasename; (2)列出了MySQL数据库管理系统中的所有可访问的数据库: SHOW DA ...
- python学习之-项目开发目录规范
软件目录结构规范有什么好处: 通过规范化,能够更好的控制软件结构,让程序具有更高的可读性. 项目目录组织结构如下: Foo/ # 项目名 --bin/ # 可执行文件目录 --foo # 可执行程序 ...
- P1396 营救 洛谷
https://www.luogu.org/problem/show?pid=1396 题目描述 “咚咚咚……”“查水表!”原来是查水表来了,现在哪里找这么热心上门的查表员啊!小明感动的热泪盈眶,开起 ...
- POJ 2337 【字典序】【欧拉回路】
题意: 给你一些单词,判断这些单词能否在保证首尾单词相同的情况下连成一排. 如果有多组解,输出字典序最小的一组解. 这题... WA了两天. 错误有以下: 1.没有初始化好起始位置,默认起始位置是a了 ...
- JavaScript 中 for 循环
在ECMAScript5(简称 ES5)中,有三种 for 循环,分别是: 简单for循环 for-in forEach 在2015年6月份发布的ECMAScript6(简称 ES6)中,新增了一种循 ...
- Codeforces Round #258 (Div. 2) B. Sort the Array(简单题)
题目链接:http://codeforces.com/contest/451/problem/B --------------------------------------------------- ...