关于python语言使用redis时,连接是否需要关闭的问题

python操作完redis,需要关闭连接的吧,怎么关闭呢

1人赞 回复

君惜丶

redis-server会关闭空闲超时的连接
redis.conf中可以设置超时时间:
timeout 300

2017.10.21 11:16 回复

君惜丶

如果使用连接池就不需要关闭。

当我们用Redis和StrictRedis创建连接时,其实内部实现并没有主动给我创建一个连接,我们获得的连接是连接池提供的连接,这个连接由连接池管理,所以我们无需关注连接是否需要主动释放的问题。另外连接池有自己的关闭连接的接口,一旦调用该接口,所有连接都将被关闭。

附:

Redis in python, how do you close the connection?

up vote 0 down vote favorite
1
https://github.com/andymccurdy/redis-py

I know in ruby we use the quit() method. I can‘t find anything here for python

python:

import redis
r = redis.StrictRedis(host=‘localhost‘, port=6379, db=0)
r.set(‘foo‘, ‘bar‘)
print r.get(‘foo‘)
#r.close() doesn‘t work
ruby

require "redis"
redis = Redis.new
redis.set("mykey", "hello world")
puts redis.get("mykey")
redis.quit()
python redis
share|improve this question
asked Jul 21 at 22:20

nevermind
555319
 
 
Looking at the source code, StrictRedis doesn‘t implement close or quit methods. – jonrsharpe Jul 21 at 22:33
 
is it okay that we don‘t close the connection, I don‘t think I understand connection to redis ... – nevermind Jul 21 at 22:39
 
@nevermind I see r.client_kill, but to find out, which client to kill, you have to list them by r.client_list(). Checking $ netstat | grep 6379 I saw, the connection got into "closing" state. There is also r.execute_command("QUIT"). But I am still not sure, if it does, what you ask for. – Jan Vlcinsky Jul 21 at 22:44
 
do we need to kill it? can I safely use StrictRedis and not worry about the connection? – nevermind Jul 21 at 23:48
add a comment |
2 Answers 2
active oldest votes
up vote 1 down vote accepted
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.

share|improve this answer
answered Jul 22 at 0:09

SpiritMachine
972411
 
 
add a comment |

up vote 0 down vote
StrictRedis doesn‘t implement connection semantics itself, instead it uses a connection pool, which is available as a property of a StrictRedis instance: S.connection_pool. The connection_pool object has a disconnect method to force an immediate disconnect of all connections in the pool if necessary, however when your StrictRedis object goes out of scope, the individual connections in the pool all clean themselves up without your intervention (see redis/connection.py:392-396)

share|improve this answer
edited Jul 22 at 7:13

answered Jul 21 at 22:41

sirlark
856615
 
 
If I decide to go with Strict, do I need to worry about the connection? – nevermind Jul 21 at 23:25
---------------------
作者:ysh_ysh
来源:CSDN
原文:https://blog.csdn.net/woshikalz/article/details/40130555
版权声明:本文为博主原创文章,转载请附上博文链接!

手动关闭

r.connection_pool.disconnect()

原文地址:https://www.cnblogs.com/ExMan/p/9807290.html

时间: 2024-08-01 07:20:50

关于python语言使用redis时,连接是否需要关闭的问题的相关文章

Python语言编写脚本时,对日期控件的处理方式

对日期控件,日期控件的输入控一般是不能手动输入的:把readonly属性去掉就好 其实很简单,我们不去搞时间日期空间,我们把它当成一个普通的input框处理就好了! 但是,很多此类型input框都是禁止手动输入的,怎么办? 很简单,用js把禁止输入的readonly属性去掉就好了.如下四种方法都可 # -*- coding: utf-8 -*- from selenium import webdriver from time import sleep driver = webdriver.Fir

安装redis,以及python如何引用redis

下载 cd /usr/local/src/ wget http://download.redis.io/releases/redis-2.8.17.tar.gz 解压 tar -zxvf redis-2.8.17.tar.gz 安装编译组件 yum install build-essential 切换到redis-2.8.17目录,编译 make (输出 Hint: It's a good idea to run 'make test' ;) )表示成功 进入目录 /home/redis/red

python中redis的连接和操作

一.redis redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorted set --有序集合)和hash(哈希类型).这些数据类型都支持push/pop.add/remove及取交集并集和差集及更丰富的操作,而且这些操作都是原子性的.在此基础上,redis支持各种不同方式的排序.与memcached一样,为了保证效率,数据都是缓存在内存中.区别的是redis会周

python 里的 redis 连接池的原理

python设置redis连接池的好处: 通常情况下,需要连接redis时,会创建一个连接,基于这个连接进行redis操作,操作完成后去释放,正常情况下,这是没有问题的,但是并发量较高的情况下,频繁的连接创建和释放对性能会有较高的影响,于是连接池发挥作用. 连接池的原理:‘预先创建多个连接,当进行redis操作时,直接获取已经创建好的连接进行操作.完成后,不会释放这个连接,而是让其返回连接池,用于后续redis操作!这样避免连续创建和释放,从而提高了性能! import redis pool =

在使用python语言的open函数时,提示错误OSError: [Errno 22] Invalid argument: ‘文件路径’

如题,在使用python语言的open函数时,提示错误OSError: [Errno 22] Invalid argument: '文件路径',在查阅了大量资料后也得到了一些解决方案,但是这些解决方案对于作者的情况都不适用,依然报错,没办法,虽然作者的英语水平很不咋地,但中文帮不了作者,只好求助于英文了. ? ? ? ?建议各位看客在修改时,仔细看清楚自己的情况是否适用.废话不多说,开始正文. ? ? ? ?作者的路径为open('D:\LearningBooks\test.txt') ? ?

Python语言及操作系统等《转》

转自:https://github.com/taizilongxu/interview_python Python语言特性 1 Python的函数参数传递 2 Python中的元类(metaclass) 3 @staticmethod和@classmethod 4 类变量和实例变量 5 Python自省 6 字典推导式 7 Python中单下划线和双下划线 8 字符串格式化:%和.format 9 迭代器和生成器 10 *args and **kwargs 11 面向切面编程AOP和装饰器 12

Python语言特性

Python语言特性 1 Python的函数参数传递 看两个例子: Python 1 2 3 4 5 a = 1 def fun(a): a = 2 fun(a) print a  # 1 Python 1 2 3 4 5 a = [] def fun(a): a.append(1) fun(a) print a  # [1] 所有的变量都可以理解是内存中一个对象的"引用",或者,也可以看似c中void*的感觉. 这里记住的是类型是属于对象的,而不是变量.而对象有两种,"可更

Python 随笔之Redis

Python学习记录 --redis 2018-03-07 Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API.从2010年3月15日起,Redis的开发工作由VMware主持.redis是一个key-value存储系统(线程安全).和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合)和zset(有序集合).这些数据类型都支持push/pop

Python语言学习笔记

获得人生中的成功需要的专注与坚持不懈多过天才与机会.  ——C.W. Wendte Python将很快成为你最喜欢的编程语言! Qt库?  PyQt  Perl-Qt 简单易学,功能强大,高效率的高层数据结构,简单而有效地实现面向对象编程. Python简洁的语法和对动态输入的支持,再加上解释性语言的本质,使得它在大多数平台上的许多领域都是一个理想的脚本语言,特别适用于快速的应用程序开发. 注重的是如何解决问题而不是编程语言的语法和结构. wxPython,Twisted,Boa Constru